Issue with WPF validation(IDataErrorInfo) and tab focusing

后端 未结 2 1878
挽巷
挽巷 2020-12-14 21:40

I have a TextBox bound to a property of an object which implements IDataErrorInfo. I set up the Validation.ErrorTemplate of the

相关标签:
2条回答
  • 2020-12-14 22:19

    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>
    
    0 讨论(0)
  • 2020-12-14 22:29

    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 (No longer exists)

    0 讨论(0)
提交回复
热议问题