- <TextBox Text="{Binding DM1.Value,UpdateSourceTrigger=PropertyChanged,
- Mode=TwoWay}" Name="textBox"/>
- <TextBox Text="{Binding DM1.Value,Mode=OneWay}" Name="tb2"/>
- <TextBox Text="{Binding DM1.Value,UpdateSourceTrigger=PropertyChanged,
- Mode=OneWayToSource}" />
- <TextBox Text="{Binding DM1.Value,
- Mode=OneTime}"/>
首先源是指后台数据,目标指,前台显示。
TwoWay:"双向奔赴"。
OneWay 后台数据 -> 前台显示,
OneWayToSource 与OneWay 相反。
OneTime 就只有初始化的时候起作用了。
UpdateSourceTrigger ,表示当前台数据(目标)发送变化时,更新源(后台数据)的时机。(注意:UpdateSourceTrigger 关心的方向是:前台显示->后台数据,所以当Mode为OneWay时,设置这个属性是没有意义的)
- //
- // 摘要:
- // Describes the timing of binding source updates.
- public enum UpdateSourceTrigger
- {
- //
- // 摘要:
- // The default System.Windows.Data.UpdateSourceTrigger value of the binding target
- // property. The default value for most dependency properties is System.Windows.Data.UpdateSourceTrigger.PropertyChanged,
- // while the System.Windows.Controls.TextBox.Text property has a default value of
- // System.Windows.Data.UpdateSourceTrigger.LostFocus.
- Default = 0,
- //
- // 摘要:
- // Updates the binding source immediately whenever the binding target property changes.
- PropertyChanged = 1,
- //
- // 摘要:
- // Updates the binding source whenever the binding target element loses focus.
- LostFocus = 2,
- //
- // 摘要:
- // Updates the binding source only when you call the System.Windows.Data.BindingExpression.UpdateSource
- // method.
- Explicit = 3
- }
表示,只要前台发送变化,立马通知源(也就是调用属性的set方法)
表示控件失去焦点时触发。
通过绑定表达式调用UpdateSource调用后触发。
- //获取绑定表达式
- BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
- //调用后触发
- bindingExpression.UpdateSource();