验证事件未触发

时间:2014-05-06 22:34:33

标签: c# wpf

我有一个非常简单的viewModel:

class ViewModel : IDataErrorInfo
{
    public Producto myProduct { get; set; }
    public string PR { get; set; }

    public ViewModel()
    {
        myProduct = new Producto { ID = 1, Name = "Product 1" };
        PR = "Test";
    }
    public string Error
    {
        get { throw new NotImplementedException(); }
    }

    public string this[string columnName]
    {
        get 
        {
            string sError = "";
            return sError;
        }
    }
}

这个简单的观点:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"   
    Title="MainWindow" Height="350" Width="525">    
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
<Grid>
    <StackPanel>
    <TextBox Height="40" Width="200" Text="{Binding Path=myProduct.Name,ValidatesOnDataErrors=True}"/>
        <TextBox Height="40" Width="200" Text="{Binding Path=PR,ValidatesOnDataErrors=True}"/>
    </StackPanel>
</Grid>

有人能告诉我为什么会为属性PR触发验证事件而不是针对myProduct触发验证事件? 我无法验证来自viewmodel的公开对象的字段!请任何人!!!

1 个答案:

答案 0 :(得分:1)

{Binding Path=myProduct.Name, …

对于使用IDataErrorInfo的绑定,myProduct的类型也必须实现IDataErrorInfo。就像你需要为子对象实现INotifyPropertyChanged一样,你也需要为每个子对象实现错误信息界面。

相关问题