Issue with WPF validation(IDataErrorInfo) and tab focusing

最后都变了- 提交于 2019-11-28 21:33:57

Tab items tend to mess up with adorners (although I don't know why, I experienced it).

I could reproduce your problem.

Solve it by wrapping the contents of the TabItem with an AdornerDecorator.

So:

<TabControl >
    <TabItem Header="tabItem1" Name="tabItem1" GotFocus="tabItem1_GotFocus">

        <AdornerDecorator>

           <Grid>
           ....
           </Grid>

        </AdornerDecorator>

    </TabItem>
    ...
</TabControl>

I had problem with only first (focused) tab got style and only that one persisted after changing. This is solution I ended up with (without AdornerDecorator)

<Style TargetType="{x:Type FrameworkElement}" x:Key="ValidatingControl">
<Style.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="Validation.HasError" Value="True" />
            <Condition Property="IsVisible" Value="True" />
        </MultiTrigger.Conditions>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder Name="controlWithError"/>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
    </MultiTrigger>
</Style.Triggers>

Based on this article: http://techqa.info/programming/question/1369643/wpf-error-styles-only-being-rendered-properly-on-visible-tab-of-a-tab-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!