0
我有一个自定义对象Foo,它具有一个名为Flagged的布尔属性,当某个人在文本框中成功输入某些内容时,它将标记为true,并用某些文本更新屏幕上的另一个文本块。问题是我可以让它在加载初始数据时工作,但是当用户成功键入某些内容来翻转标志时什么都不会发生。WPF更新源触发器实现
我必须在代码背后做大部分代码,并且在我的对象Foo上实现了INotifyPropertyChanged。我的代码在下面有什么问题?
谢谢。
private Border CreateNewBorder()
{
Border b = new Border();
TextBlock tb = new TextBlock();
tb.TextAlignment = TextAlignment.Center;
b.Style = (Style)this.FindResource("myBorder");
tb.SetBinding(TextBlock.TextProperty, CreateBinding());
b.Child = tb;
return b;
}
private Binding CreateBinding()
{
Binding bind = new Binding();
bind.Source = Foo;
bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bind.Mode = BindingMode.TwoWay;
bind.Path = new PropertyPath("Flagged");
bind.Converter = new BoolToStringConverter();
return bind;
}
您是否在调试器中验证了在用户键入时Flagged已设置为true? Foo对象用作CreateBinding()中的Source是否与用户输入时正在更新的实例相同?你可以发布Foo代码的相关位吗? – itowlson 2010-03-16 02:15:31
另请注意,在TextBlock.Text绑定上设置UpdateSourceTrigger(或Mode = TwoWay)没有意义 - 用户无法键入TextBlock,因此永远不会有任何更新推回到绑定源。 – itowlson 2010-03-16 02:17:06