WPF-ValidationRule没有被调用 [英] WPF - ValidationRule is not being called

查看:175
本文介绍了WPF-ValidationRule没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个TextBlock Xaml:

I got this Xaml of a TextBlock:

<TextBlock VerticalAlignment="Center">
       <TextBlock.Text>
             <Binding Path="FilesPath" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                  <Binding.ValidationRules>
                        <viewModel:ExtensionRule></viewModel:ExtensionRule>
                   </Binding.ValidationRules>
              </Binding>
         </TextBlock.Text>
 </TextBlock>

在ViewModel中:

In the ViewModel:

    private string _filesPath;
    public string FilesPath
    {
        set 
        { 
            _filesPath = value;
            OnPropertyChange("FilesPath");
        }
        get { return _filesPath; }
    }

    private void OnPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

,验证规则是这样:

public class ExtensionRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        string filePath = String.Empty;
        filePath = (string)value;
        if (String.IsNullOrEmpty(filePath))
        {
            return new ValidationResult(false, "Must give a path");
        }

        if (!File.Exists(filePath))
        {
            return new ValidationResult(false, "File not found");
        }
        string ext = Path.GetExtension(filePath);
        if (!ext.ToLower().Contains("txt"))
        {
            return new ValidationResult(false, "given file does not end with the \".txt\" file extenstion");
        }
        return new ValidationResult(true, null);
    }
}

并且另一个事件正在更新FilesPath属性:(vm是viewModel变量)

and the FilesPath property is being updated by another event: (vm is the viewModel var)

private void BrowseButton_Click(object sender, RoutedEventArgs e)
    {
        // Create OpenFileDialog 
        OpenFileDialog dlg = new OpenFileDialog();

        // Set filter for file extension and default file extension 
        dlg.DefaultExt = ".txt";
        dlg.Filter = "txt Files (*.txt)|*.txt";

        // Display OpenFileDialog by calling ShowDialog method 
        bool? result = dlg.ShowDialog();

        // Get the selected file name and display in a TextBox 
        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            vm.FilesPath = filename;
        }
    }

为什么通过文件对话框选择文件时未调用ValidationRule?

Why doesn't the ValidationRule being called when i choose a file by the file dialog?

推荐答案

根据

According to this MSDN Library article validation rules are only checked when transferring data from binding's target property (TextBlock.Text in your case) to source property (your vm.FilesPath property) - the aim here is to validate user input from, for example, a TextBox. In order to give validation feedback from source property to the control owning the target property (the TextBlock control) your view model should implement either IDataErrorInfo or INotifyDataErrorInfo.

这篇关于WPF-ValidationRule没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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