验证ItemTemplate内部的Control

时间:2011-12-20 13:55:44

标签: wpf validation itemscontrol itemtemplate

我有这样的问题: 我想强制用户为添加到我的集合中的每个元素为ItemTemplate内部的控件选择一个值。

<ItemsControl ItemsSource="{Binding MySourceList}" ItemTemplateSelector="{StaticResource mySelector}"  />

MySourceListMyObject个对象的可观察集合。

public class MyObject : INotifyPropertyChanged, IDataErrorInfo
{

    ...

    public string Error
    {
        get { return ""; }
    }

    public string this[string columnName]
    {
        get 
        {
            string result = null;

            if (columnName == "MyColumn")
            {
                if (MyColumn == null) result = "Error empty field";
            }                       

            return result;                    
        }
    }
}


<SomeControl.SelectedItem>
    <Binding Path="MyColumn" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"  NotifyOnValidationError="True" />    
</SomeControl.SelectedItem>

因此,如果MySourceList中添加了新的MyObject,则会出现验证错误,这是正常的。 问题是当从MySourceList中删除具有验证错误的对象(MyColumn为null)时,此控件的Error集合 我们不会意识到这一点,并且Control认为存在一些验证错误。知道怎么解决吗?

谢谢

0 个答案:

没有答案
相关问题