【问题标题】:WPF Data Binding with StringFormat when UpdateSourceTrigger is PropertyChanged当 UpdateSourceTrigger 为 PropertyChanged 时 WPF 数据与 StringFormat 绑定
【发布时间】:2010-12-29 12:24:18
【问题描述】:

我想让一个文本框在用户跳出控件时附加一个特定的字符串,即 LostFocus,但是我更喜欢文本框在用户键入时进行验证,所以 UpdateSourceTrigger 设置为 PropertyChanged

有没有办法让它在 WPF 中工作?

查看了类似的question,但想知道是否有更清洁的解决方案?

我的 XAML 是:

    <TextBox Text="{Binding Path=MyBindingPath, 
                            StringFormat='\{0} -HELLO',
                            TargetNullValue={x:Static sys:String.Empty},
                            ValidatesOnDataErrors=True,   
                            NotifyOnValidationError=True,    
                            UpdateSourceTrigger=PropertyChanged}"/>

【问题讨论】:

    标签: wpf binding properties string-formatting updatesourcetrigger


    【解决方案1】:

    您可以将 UpdateSourceTrigger 设置为 Explicit,并且在 TextBox 的 TextChanged 事件处理程序中,您可以在执行所需操作后显式调用 UpdateSource。

    //write the code you want to run first and then the following code
    BindingExpression exp = this.textBox1.GetBindingExpression(TextBox.TextProperty);
    exp.UpdateSource();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-08
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多