How can I indicate in the Expander header that the collapsed content has an error

I have expanders that contain textboxes, textboxes use wpf validation stuff to draw a red border around them (textboxes are wrapped in Adorner Decorators to make sure I don't get empty red boxes all over the place when the expanders collapse)

I want to indicate in the header of the expander that it has content that has errors (in case it is folded) - an icon or a red exclamation mark or whatever. I think I can see a way to do this in code from within my validation function (not perfect), but is there a way to do this in xaml? Can I style the expander with a trigger somehow pointing to Validation.HasError for all children?

thanks for any thoughts ..

Trev

+1


source to share


1 answer


If you know the content of your expander, you can use a MultiDataTrigger to do this:

<Expander>
    <Expander.Header>
        <TextBlock>
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Text" Value="ERROR"/>
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=txtWidth, Path=(Validation.HasError)}" Value="False"/>
                                <Condition Binding="{Binding ElementName=txtHeight, Path=(Validation.HasError)}" Value="False"/>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Text" Value="NO ERROR"/>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Expander.Header>
    <StackPanel>
        <TextBox x:Name="txtWidth" Text="{Binding Width, ElementName=rect, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
        <TextBox x:Name="txtHeight" Text="{Binding Height, ElementName=rect, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
        <Rectangle x:Name="rect" Width="100" Height="100" Margin="10" Fill="Green"/>
    </StackPanel>
</Expander>

      



If you don't know the content of the extender, you probably have to set the Binding.NotifyOnValidationError to the TextBoxes and handle the error event.

+2


source







All Articles