0

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

WPF - ValidationRule 没有被调用

在那里我被告知我应该实施INotifyDataErrorInfo,所以我做到了,但它仍然不起作用。

这是xaml:

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

在视图模型中:

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 然后更新 ViewModel 的 Path 属性的按钮。

当 TextBlock 更新时,绑定本身会起作用并调用 set 属性,但不会调用验证规则本身。这里有什么遗漏/错误?

4

1 回答 1

0

如评论中所述,ValidationRule仅在从更新绑定的情况下才会调用Target(文本块文本)Source(视图模型属性)

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

于 2014-06-21T15:09:23.977 回答