UpdateSourceTrigger =明确 [英] UpdateSourceTrigger=Explicit

查看:61
本文介绍了UpdateSourceTrigger =明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有多个文本框的WPF窗口,当用户按下确定"按钮时,我希望所有文本框都被评估为非空白.我知道我必须将TextBoxes与"UpdateSourceTrigger of'Explicit"一起使用,但是是否需要为它们每个调用"UpdateSource()"?例如

I'm creating a WPF window with multiple textboxes, when the user presses the OK button I want all the text boxes to be evaluated for being non-blank. I understand that I have to use TextBoxes with 'UpdateSourceTrigger of 'Explicit', but do I need to call 'UpdateSource()' for each of them ? e.g.

<TextBox Height="23" 
     HorizontalAlignment="Left" 
     Margin="206,108,0,0" 
     Text="{Binding Path=Definition, UpdateSourceTrigger=Explicit}"
     Name="tbDefinitionFolder" 
     VerticalAlignment="Top" 
     Width="120" />

<TextBox Height="23" 
     HorizontalAlignment="Left" 
     Margin="206,108,0,0" 
     Text="{Binding Path=Release, UpdateSourceTrigger=Explicit}"
     Name="tbReleaseFolder" 
     VerticalAlignment="Top" 
     Width="120" />

...

BindingExpression be = tbDefinitionFolder.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
BindingExpression be2 = tbReleaseFolder.GetBindingExpression(TextBox.TextProperty);
be2.UpdateSource();

推荐答案

另一种方法是将UpdateSourceTrigger设置为PropertyChanged.

An alternative approach can be to set your UpdateSourceTrigger to PropertyChanged.

,然后从INotifyPropertyChanged和IDataErrorInfo继承您的VM.这是一个例子...

And then inherit your VM from both INotifyPropertyChanged and IDataErrorInfo. Here's an example...

public class MyViewModel : INotifyPropertyChanged, IDataErrorInfo
{
    private string myVar;
    public string MyProperty
    {
        [DebuggerStepThrough]
        get { return myVar; }
        [DebuggerStepThrough]
        set
        {
            if (value != myVar)
            {
                myVar = value;
                OnPropertyChanged("MyProperty");
            }
        }
    }
    private void OnPropertyChanged(string prop)
    {
        if(PropertyChanged!=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(pro));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public string Error
    {
        get { return String.Empty; }
    }
    public string this[string columnName]
    {
        get
        {
            if (columnName == "MyProperty")
            {
                if (String.IsNullOrEmpty(MyProperty))
                {
                    return "Should not be blank";
                }
            }
            return null;
        }
    }
}

假定您的一个文本框已绑定到如上所述的"MyProperty".索引器在IDataErrorInfo中实现,并且在"MyProperty"更改时被调用.在索引器主体中,您可以检查该值是否为空并返回错误字符串.如果错误字符串为非null,则用户会在TextBox上获得漂亮的装饰物作为视觉提示.因此,您可以轻松完成验证并提供UI体验.

Assume that one of your TextBoxes is bound to 'MyProperty' as declared above. The indexer is implemented in IDataErrorInfo, and gets called when 'MyProperty' changes. In the indexer body, you can perform a check if the value is empty and return an error string. If the error string is non-null, the user gets a nice adorner on the TextBox as a visual cue. So you are in one shot performing validation and delivering the UI experience.

如果您使用上面编码的两个接口并使用UpdateSourceTrigger = PropertyChanged,则所有这些都是免费的.使用UpdateSourceTrigger = Explicit对于提供您描述的验证是过大的.

All of this is for free if you use the two interfaces as coded above and use UpdateSourceTrigger=PropertyChanged. The use of UpdateSourceTrigger = Explicit is massive overkill for providing the validation you described.

文本框的Xaml是...

The Xaml for the TextBox would be...

 <TextBox DataContext="{StaticResource Vm}" Text="{Binding MyProperty,
                UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
                NotifyOnSourceUpdated=True, Mode=TwoWay}" Width="200" Height="25"/>

这篇关于UpdateSourceTrigger =明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆