[英]Two-Way Binding is not working
在我的Silverlight项目中,我正在创建文本框,这些文本框在运行时是双向绑定到某些Context的数据。 在一个方向( 从源到目标 )的绑定似乎工作正常,但是在另一个方向( 从目标到源 )的绑定没有显示任何效果。
这是数据上下文:
public class Leg : INotifyPropertyChanged {
private string passengers;
public string Passengers {
get { return passengers; }
set {
// here I have a breakpoint.
passengers = value;
FirePropertyChanged("Passengers");
}
}
private void FirePropertyChanged (string property) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
然后在另一个地方,我将创建一个新的TextBox控件及其绑定:
Binding passengersBinding = new Binding();
// viewModelLeg is an instance of the class Leg from above
passengersBinding.Source = viewModelLeg;
passengersBinding.Path = new PropertyPath("Passengers");
passengersBinding.Mode = BindingMode.TwoWay;
legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);
现在,当我更改乘客字符串的值时,绑定到它的相应文本框将正确更新其文本。 所以这里一切都很好。
但是,当我手动更改文本框的文本,然后使文本框失去焦点时,什么也没发生-即没有双向绑定发生-文本框的新文本值没有向下传播到源!
我在旅客属性的设置者处有一个断点(上面标有断点注释)。 当我一切准备就绪时,当绑定的目标值已更改以更新源时,绑定引擎也会使用此公共设置器-因此,在这种情况下,必须命中断点。 但是他没有! 因此,似乎我可以用自己的文本框(按焦点播放或按Enter键)执行我想做的事情,但它从未更新其源代码。
我在监督什么吗? 在我的代码或思想中都必须有一个大写错误。.我将非常感谢任何想法...
编辑:
在下面的内容中,我尝试演示如何创建XAML对象和DataContext对象。 因为我在运行时创建XAML控件及其绑定,所以我没有找到很好的解决方案来很好地实现MVVM方法。 因此,我正在执行以下操作(这可能不是最好的方法):
我正在建模的情况是,我有一个(主要是)文本框组成的UserControl(称为LegItem)。 在运行时,用户可以根据自己的意愿(一个接一个)创建尽可能多的userControl。
在我的ViewModel端,我有一个类(称为Leg),可以用作一个LegItem的ViewModel。 因此,当我说n(XAML-)LegItems时,我也有n个Leg实例。 我将这些Leg对象存储在一个List中。
因此,每当用户单击“添加新的腿”按钮时,我都会执行以下操作:
// here we are inside the applications view in an .xaml.cs file
public void AddLeg () {
// this is going to serve as the ViewModel for the new LegItem
// I am about to create.
Leg leg = viewModel.insertLeg();
// here I am starting to create the visual LegItem. The ViewModel object
// I have created in the previous step is getting along with.
createLegItem(leg);
}
// the primary job here is to bind each contained textbox to its DataContext.
private LegItem createLeg (Leg viewModelLeg) {
// create the visual leg item control element
// which is defined as a XAML UserControl.
LegItem legItem = new LegItem();
Binding passengersBinding = new Binding();
// viewModelLeg is an instance of the class Leg from above
passengersBinding.Source = viewModelLeg;
passengersBinding.Path = new PropertyPath("Passengers");
passengersBinding.Mode = BindingMode.TwoWay;
legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);
}
// on the viewModel side there is this simple method that creates one Leg object
// for each LegItem the View is creating and stores inside a simple list.
public Leg InsertLeg () {
Leg leg = new Leg();
legList.add(leg)
return leg;
}
新答案
由于您提到绑定实际上是绑定到自定义UserControl
而不是TextBox
,因此建议您查看UserControl
的XAML并确保其正确绑定了数据
旧答案
我对一个新的Silverlight项目进行了快速测试,并注意到启动项目是SilverlightApplication1.Web
,而不是SilverlightApplication
。
这意味着在我运行项目时,setter中的断点实际上不会被命中。 您会注意到断点圆圈只是轮廓,而颜色并未填充。如果将鼠标悬停在其上,它将说出
断点当前不会被命中。 没有为该文档加载任何符号
如果我启动SilverlightApplication1
而不是.Web
版本, .Web
断点。
该酒店得到改变正确不管我启动的版本,但是如果我开始与项目中的断点没有击中.Web
版本。 我怀疑这是您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.