ValidatesOnExceptions与ValidatesOnDataErrors冲突

时间:2017-06-30 13:30:24

标签: wpf validation

我是WPF的新手,目前正在学习验证。 在我的简单示例中,我有这个Model类:

class Book
{
    public int Id
            {
                get { return id; }
                set
                {
                    if (id == value) return;
                    id = value;
                    OnPropertyChanged(nameof(Id));
                }
            }

            public string Title
            {
                get { return title; }
                set
                {
                    if (title == value) return;
                    title = value;
                    OnPropertyChanged(nameof(Title));
                    OnPropertyChanged(nameof(PagesCount));
                }
            }
            public int PagesCount
            {
                get { return pagesCount; }
                set
                {
                    if (pagesCount == value) return;
                    pagesCount = value;
                    OnPropertyChanged(nameof(Title));
                    OnPropertyChanged(nameof(PagesCount));
                }
            }
            public string Color
            {
                get { return color; }
                set
                {
                    if (color == value) return;
                    color = value;
                    OnPropertyChanged(nameof(Color));
                }
            }
}

我实施了IDataErorInfoINotifyDataErorInfo。 我的一个验证规则(为测试而创作)是用户无法同时在标题中输入SS和在PagesCount中输入33,因此我在INotifyDataErrorInfo的索引器中添加了这个:

case nameof(Title):
                        if (Title == "N")
                        {
                            AddError(nameof(Title), "Annoying");
                            hasError = true;
                        }
                        if (Title == "SS" && PagesCount == 33)
                        {
                            AddError(nameof(Title), "SS and 33 not compatible");
                            hasError = true;
                        }
                        if (!hasError)
                            ClearErrors(nameof(Title));
                        break;
                    case nameof(PagesCount):
                        if (Title == "SS" && PagesCount == 33)
                        {
                            AddError(nameof(Title), "SS and 33 not compatible");
                            hasError = true;
                        }
                        if (!hasError)
                            ClearErrors(nameof(Title));

                        break;

当我输入这些值(标题为SS,PagesCount为33)时,会出现红色边框,并在此模板中显示错误消息:

<ListBox Grid.Row="8" Grid.ColumnSpan="2" ItemsSource="{Binding ElementName=mGrd, Path=(Validation.Errors)}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding Path=ErrorContent}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

但是当我输入PagesCount的无效值(无效int),如 33e 时,错误消息不会从ListBox中消失,并且边框仍会装饰两个文本框。 我不知道为什么它不清除错误模板中的错误消息,只保留红色边框,所以有没有办法做到这一点,我也可以显示这些异常的消息(输入 33e < / strong> for int field)

1 个答案:

答案 0 :(得分:0)

ValidatesOnExceptions属性设置为true是将内置ExceptionValidationRule添加到绑定的ValidationRules集合的简称:

<TextBox>
    <TextBox.Text>
        <Binding Path="Age" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

这样做是在设置源属性(在这种情况下为ValidationError)时抛出异常时会向Validation.Errors集合添加Age对象。这将导致显示元素Validation.ErrorTemplateTextBox)。

因为你永远不会int属性设置为“33e”,因为这不是有效的int值,所以在这种情况下确实会抛出异常