WPF - ValidationRule没有被调用

时间:2014-06-21 14:45:37

标签: c# wpf xaml validationrule

这是我之前提出的问题的后续问题:

WPF - ValidationRule is not being called

有人告诉我应该实施INotifyDataErrorInfo,所以我做了但是它仍然没有用。

这是xaml:

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

在ViewModel中:

private string _path;
    public string Path
    {
        set 
        { 
            _path = value;
            OnPropertyChange("Path");
        }
        get { return _path; }
    }

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

    private Dictionary<String, List<String>> errors = new Dictionary<string, List<string>>();
    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    private void SetErrors(string propertyName, List<string> propertyErrors)
    {
        // Clear any errors that already exist for this property.
        errors.Remove(propertyName);
        // Add the list collection for the specified property.
        errors.Add(propertyName, propertyErrors);
        // Raise the error-notification event.
        if (ErrorsChanged != null)
            ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
    }
    private void ClearErrors(string propertyName)
    {
        // Remove the error list for this property.
        errors.Remove(propertyName);
        // Raise the error-notification event.
        if (ErrorsChanged != null)
            ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
    }

    public System.Collections.IEnumerable GetErrors(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
        {
            // Provide all the error collections.
            return (errors.Values);
        }
        else
        {
            // Provice the error collection for the requested property
            // (if it has errors).
            if (errors.ContainsKey(propertyName))
            {
                return (errors[propertyName]);
            }
            else
            {
                return null;
            }
        }
    }

    public bool HasErrors
    {
        get
        {
            return errors.Count > 0;
        }
    }

验证规则:

 public class StrRule : 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");
        }
        return new ValidationResult(true, null);
    }
}

我还有一个打开FileDialog的按钮,然后更新ViewModels&#39;路径属性。

更新TextBlock时,绑定本身有效并且正在调用set属性,而不是验证规则本身。这里有什么遗漏/错误?

1 个答案:

答案 0 :(得分:0)

正如评论 ValidationRule 中所述,只有在 Target (TextBlock Text) Source (ViewModel属性)

因此,不是直接设置源值vm.FilesPath = filename;,而是设置TextBlock Text值和验证规则 Validate 方法。