質問

I have text boxes and combo boxes with validation. I want to show a certain style when there is an invalid value and a different style when the box is empty, like at startup. Technically, an empty box is an error because I need to disable a button until the boxes contain valid values.

Is there a way to set different styles based on the validation error or am I going to need make null values not errors and control the button with more than simple validation to see if the rest of the controls are valid?

I am currently using this style for all errors including null values.

<Style x:Key="Error" TargetType="{x:Type Control}">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="0,2,40,2" />
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="true">
                        <Border Background="Red" DockPanel.Dock="left" Margin="5,0,0,0" 
                            Width="20" Height="20" CornerRadius="10"
                            ToolTip="{Binding ElementName=customAdorner, 
                                      Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                            <TextBlock Text="X" VerticalAlignment="center" HorizontalAlignment="center" Foreground="white" />
                        </Border>
                        <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
                            <Border/>
                        </AdornedElementPlaceholder>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
役に立ちましたか?

解決

I think you want different error template or visual effects on different errors. Is that right? If so following post may be what you are after.

Multiple validation rules

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top