【wpf】Bingding的方向和触发的时机

<!--两个方向都可行-->
<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}"/>

Mode

首先源是指后台数据,目标指,前台显示。

TwoWay:"双向奔赴"。

OneWay 后台数据 -> 前台显示,

OneWayToSource 与OneWay 相反。

OneTime 就只有初始化的时候起作用了。

UpdateSourceTrigger

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
}

PropertyChanged 

表示,只要前台发送变化,立马通知源(也就是调用属性的set方法)

LostFocus 

表示控件失去焦点时触发。

Explicit 

通过绑定表达式调用UpdateSource调用后触发。

//获取绑定表达式
BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
//调用后触发
bindingExpression.UpdateSource();

posted @ 2022-08-08 11:38  宋桓公  阅读(70)  评论(0编辑  收藏  举报