MetroWindow 中的数据验证问题

Data validation issue in MetroWindow

我是 Wpf 的新手,我正在尝试各种控件。我从 Metro Mahapps Demo 中获取了这个 ViewModel,只留下(假设我没有遗漏任何东西)处理数据验证的代码(数量大于 10):

 public class Modelis : INotifyPropertyChanged, IDataErrorInfo
{
    int? _integerGreater10Property;

    public Modelis(IDialogCoordinator dialogCoordinator)
    {    
    }
    public int? IntegerGreater10Property
    {
        get { return this._integerGreater10Property; }
        set
        {
            if (Equals(value, _integerGreater10Property))
            {
                return;
            }

            _integerGreater10Property = value;
            RaisePropertyChanged("IntegerGreater10Property");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises the PropertyChanged event if needed.
    /// </summary>
    /// <param name="propertyName">The name of the property that changed.</param>
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string this[string columnName]
    {
        get
        {
            if (columnName == "IntegerGreater10Property" && this.IntegerGreater10Property < 10)
            {
                return "Number is not greater than 10";
            }
            return null;
        }
    }

    public string Error { get { return string.Empty; } }
}

然后,我在 Window 中引用了它(也取自演示):

         xmlns:Registracija="clr-namespace:Registracijos_sistema"
         mc:Ignorable="d"
         d:DesignHeight="600"
         d:DesignWidth="800"
         d:DataContext="{d:DesignInstance Registracija:Modelis}"

但是当我添加一个文本框(再次来自演示)来通知数字是否不大于 10 时,它似乎不起作用:

        <TextBox   
        Controls:TextBoxHelper.Watermark="Number smaller than 10"
        Text="{Binding IntegerGreater10Property, 
        ValidatesOnDataErrors=True, 
        UpdateSourceTrigger=PropertyChanged, 
        NotifyOnValidationError=True}"  
        Margin="104,197,47,104" />

我做错了什么?

尝试将以下内容添加到您的 MetroWindow xaml:

    xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
    Dialog:DialogParticipation.Register="{Binding}"

你的 window 代码背后是否有依赖项 属性,就像这样?

    public Modelis MyModelis
    {
        get { return (Modelis)GetValue(MyModelisProperty); }
        set { SetValue(MyModelisProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyModelis.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyModelisProperty =
        DependencyProperty.Register("MyModelis", typeof(Modelis), typeof(Window1), new PropertyMetadata(new Modelis(DialogCoordinator.Instance)));

提示:如果您使用的是 Visual Studio,您可以键入 propdp 并点击 'TAB' 让它自动添加代码。之后,只需输入缺失值即可。

然后回到我们的 MetroWindow xaml,更改为:

d:DataContext="{d:DesignInstance Registracija:Modelis}"

对此:

 DataContext="{Binding MyModelis, RelativeSource={RelativeSource Self}}"