当前位置: 首页 > 编程语言 > C#

从字符串到文本框的XAML绑定不起作用Share

时间:2023-04-10 10:43:58 C#

XAML从字符串到文本框的绑定不起作用好好工作。我正在关注Microsoft的数据绑定概述。我有一个最终将用作状态窗口的TextBox。现在我只想能够向其中写入任意文本字符串。我还有一个正在测试的命令模式实例。我的命令涉及将随机数添加到累加器并将结果打印到状态视图。这是我的主应用程序窗口的XAML:以及表示我的数据的类:usingSystem;使用System.Collections.Generic;使用System.Linq;使用系统文本;namespaceExperiment{publicclassStatusViewText{publicStringBuilderstatusText{get;放;}publicStringstatusTextString{得到;放;}publicStatusViewText(){statusText=newStringBuilder();}publicvoidAppend(strings){if(s!=null){statusText.Append(s);}statusTextString=statusText.ToString();}publicvoidAppendLine(strings){if(s!=null){statusText.AppendLine(s);}statusTextString=statusText.ToString();}}}最后,我将在StringBuilder中使用合适的转换器,但我想在探索这种复杂性之前先解决这个问题。如果我的理解是正确的(显然不是),那么这一切都应该有效。绑定目标是TextBox,目标属性是Text属性。绑定源是StatusViewText类的statusTextString属性。但是,当我运行命令(并调试并看到正在更新的StatusViewText.statusTextString)时,TextBox不会更新。我想我可能需要自己显式添加绑定,所以我尝试在主窗口构造函数中的InitializeComponent()之后添加它:statusViewText=newStatusViewText();绑定statusViewTextBinding=newBinding("statusTextString");statusViewTextBinding.Source=statusViewText;statusText.SetBinding(TextBlock.TextProperty,statusViewTextBinding);但这也不管用。我需要触发PropertyChanged事件吗?我认为绑定框架的全部内容是事件在幕后自动触发和消费;但也许我也错了。输出窗口中没有明显的错误,这让我相信我的绑定语法是正确的;我只是想念别的东西。砰!编辑13:14EDT谢谢mben:好的,我做到了。添加以下内容:publicclassStatusViewText:INotifyPropertyChanged{publicvoidAppend(strings){if(s!=null){statusText.Append(s);}statusTextString=statusText.ToString();NotifyPropertyChanged("statusTextString");}publicvoidAppendLine(strings){if(s!=null){statusText.AppendLine(s);}statusTextString=statusText.ToString();NotifyPropertyChanged("statusTextString");}公共事件PropertyChangedEventHandlerPropertyChanged;privatevoidNotifyPropertyChanged(Stringinfo){if(PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs(info));}}}我调试并验证了它是通过此代码路径实现的,确实如此。但仍然没有运气。还有其他想法吗?您仍然需要在StatusViewText上实现INotifyPropertyChanged。绑定系统不会不断地检查值,你需要在事情发生变化时通知它。以下是您必须应用才能使其工作的更改。这是ViewModel的代码:publicclassStatusViewText:INotifyPropertyChanged{publicStatusViewText(){//至少,我有一个默认值this.StatusTextString="Helloworld";}公共事件PropertyChangedEventHandlerPropertyChanged;privatevoidOnPropertyChanged(stringpropertyName){if(this.PropertyChanged!=null)this.PropertyChanged(this,newPropertyChangedEventArgs(propertyName));}私有字符串statusTextString;公共字符串StatusTextString{get{返回this.statusTextString;}设置{this.statusTextString=value;this.OnPropertyChanged("StatusTextString");}}}您必须指定窗口的DataContext。代码隐藏是一种解决方案:在MainWindow的ctor中,填写数据上下文:this.DataContext=newStatusViewText();在XAML中,您应该声明对属性StatusTextString的绑定。它起作用是因为数据上下文是StatusViewText的一个实例。Text="{BindingPath=StatusTextString}"请注意,您在代码中创建的实例与在Xaml中表示的实例不同。您可以通过在StatusViewText的构造函数中设置断点来证明这一点。将此代码段放入主窗口的构造函数中...StatusViewTexto=(StatusViewText)FindResource("statusViewText")asStatusViewText;if(o!=null){o.Append("你好");这将影响您正确的类实例。你也应该改变你的类看起来像这样...以上是C#学习教程:从字符串到TextBox的XAML绑定不起作用如果它对任何人有用并且需要了解更多关于C#我希望你会更多关注教程—publicclassStatusViewText:INotifyPropertyChanged{publicStringBuilderstatusText{get;放;}私有字符串_statusTextString;publicStringstatusTextString{get{return_statusTextString;}设置{_statusTextString=值;}}publicStatusViewText(){statusText=newStringBuilder();}publicvoidAppend(strings){if(s!=null){statusText.Append(s);}statusTextString=statusText.ToString();}publicvoidAppendLine(strings){if(s!=null){statusText.AppendLine(s);}statusTextString=statusText.ToString();}公共事件PropertyChangedEventHandlerPropertyChanged;privatevoidOnPropertyChanged(stringprop){if(PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs(prop));}}}本文收集自网络,不代表表态,如涉及侵权,请点击右侧联系管理员删除如转载请注明出处: