xmal文件中实现方法:
<Label x:Name="label" Height="40" Content="xxx"> </Label>
<TextBox x:Name="textBox" Text="{Binding ElementName=label, Path=Content , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
后台代码实现方法:
Binding binding = new Binding();
binding.Source = this.label;
binding.Path = new PropertyPath("Content");
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
this.textBox.SetBinding(TextBox.TextProperty , binding);
其中当双向绑定时候,设计到得UpdateSourceTrigger 绑定更新问题:
无论是哪个绑定模式 在源发生变化,变化会立即传递给目标;
双向绑定时候,目标发生变化,不一定会立即传递到源中,
而是根据UpdateSourceTrigger 的设置来选择传递方式:
default:多数的依赖属性的默认属性为PropertyChanged, Text属性模式值为LostFocus
PropertyChanged:当绑定目标属性更改时候,立即更新源;
LostFocus:当绑定目标元素失去焦点,立即更新源
Explicit:仅仅在调用UpDateSource()方法时更新绑定源;