none
IDataErrorInfo的错误信息在切换TabItem之后不再显示 RRS feed

  • 问题

  • ViewModel里面只有两个属性X\Y,实现了IPropertyChanged和IDataErrorInfo两个接口。当X+Y>10时,在索引器(IDataErrorInfo)中提交错误信息。

    两个属性分别绑定到了两个TextBox,且TextBox的ValidatesOnDataErrors设置为true。

    X\Y设置了初始值使得一开始就触发验证错误,界面显示正常,两个TextBox都标红。但是,当切换到另一个TabItem,再回切时,红色标记消失了,再次更改TextBox中的值也不在触发DataValidation。

    请问这个问题如何解决?

    完整项目:

    https://github.com/ShannonZ/playground


    2018年3月9日 8:43

答案

  • 你好,

    请使用AdornerDecorator包装TabItem的内容。

      <TabControl.Items>
                    <TabItem Header="tab1">
                        <AdornerDecorator>
    
                            <StackPanel>
                            <TextBox Margin="10,10,200,10"  Text="{Binding Path=Name, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}">
                                <TextBox.Style>
                                    <Style TargetType="{x:Type TextBox}">
                                        <Setter Property="Validation.ErrorTemplate">
                                            <Setter.Value>
                                                <ControlTemplate>
                                                    <DockPanel LastChildFill="True">
                                                        <TextBlock DockPanel.Dock="Right" 
                                           Foreground="Red" 
                                           Margin="5,0,5,0"
                                           Text="*"/>
                                                        <Border BorderBrush="Red" BorderThickness="1">
                                                            <AdornedElementPlaceholder Name="ph"/>
                                                        </Border>
                                                    </DockPanel>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                        <Style.Triggers>
                                            <Trigger Property="Validation.HasError" Value="true">
                                                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                            <TextBox Margin="10"></TextBox>
                        </StackPanel>
                        </AdornerDecorator>
                    </TabItem>
           
                <TabItem Header="tab2" >
                    <TextBox Margin="31"/>
                </TabItem>

    Best Regards,

    Bob

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年3月12日 1:45
    版主

全部回复

  • 你好,

    请使用AdornerDecorator包装TabItem的内容。

      <TabControl.Items>
                    <TabItem Header="tab1">
                        <AdornerDecorator>
    
                            <StackPanel>
                            <TextBox Margin="10,10,200,10"  Text="{Binding Path=Name, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}">
                                <TextBox.Style>
                                    <Style TargetType="{x:Type TextBox}">
                                        <Setter Property="Validation.ErrorTemplate">
                                            <Setter.Value>
                                                <ControlTemplate>
                                                    <DockPanel LastChildFill="True">
                                                        <TextBlock DockPanel.Dock="Right" 
                                           Foreground="Red" 
                                           Margin="5,0,5,0"
                                           Text="*"/>
                                                        <Border BorderBrush="Red" BorderThickness="1">
                                                            <AdornedElementPlaceholder Name="ph"/>
                                                        </Border>
                                                    </DockPanel>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                        <Style.Triggers>
                                            <Trigger Property="Validation.HasError" Value="true">
                                                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                            <TextBox Margin="10"></TextBox>
                        </StackPanel>
                        </AdornerDecorator>
                    </TabItem>
           
                <TabItem Header="tab2" >
                    <TextBox Margin="31"/>
                </TabItem>

    Best Regards,

    Bob

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年3月12日 1:45
    版主
  • Hi,

    不知道是不是bug,还是有意为之.

    当切换标签页时,用于显示Validation .Error的Adorner图层会被丢弃。

    AdornerDecorator是一个容器, 使用AdornerDecorator就是为了承载这个Adoener,使其显示到Visual Tree上.

    https://stackoverflow.com/questions/9907075/textbox-with-validation-loses-errortemplate-on-tab-change

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年3月14日 1:43
    版主