0

One can only use a ValidationRule with binding; what's a good way to use Validation.ErrorTemplate with Grid based upon its contents rather than its properties like width, etc.?

4

1 回答 1

0

Use the Tag property.

I had a group of radio buttons inside my grid that I wanted to ensure had at least one checked member. If not, I wanted to highlight the entire grid with an Validation.ErrorTemplate to alert the user that they had not finished a section of the form they are filling out.

So I did this in XAML:

<Grid
    Name="GridReportType"
    Margin="0,20,0,0">
    <Grid.Tag>
        <Binding Path="ReportTypeSelected" 
            UpdateSourceTrigger="PropertyChanged" 
            Mode="OneWayToSource">
            <!--Validation rules only fire when target (WPF property) updates source (class property)-->
            <Binding.ValidationRules>
                <parent:FailIfFalse/>
            </Binding.ValidationRules>
        </Binding>
    </Grid.Tag>
</Grid

with this in the window's code behind constructor:

this.GridReportType.Tag = false;

and with this ValidationRule:

public class FailIfFalse : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if (!((bool)value))
        {
            return new ValidationResult(false, "Required");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
}

and with this error template:

<Grid.Style>
    <Style>
        <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                <DockPanel>
                    <TextBlock DockPanel.Dock="Bottom"
                       Foreground="Red"
                       FontSize="24"
                       FontStyle="Italic"
                       HorizontalAlignment="Center"
                       Text="{Binding ElementName=cantBeEmptyAdorner,
                        Path=AdornedElement.(Validation.Errors),
                        Converter={StaticResource GetLatestValidationError}}"/>
                    <Border BorderBrush="Red"
                        BorderThickness="1"
                        Margin="-1">
                    <AdornedElementPlaceholder x:Name="cantBeEmptyAdorner"/>
                    </Border>
                </DockPanel>
                </ControlTemplate>
            </Setter.Value>
            </Setter>
        </Trigger>
        </Style.Triggers>
    </Style>
</Grid.Style>
于 2020-08-14T22:45:30.673 回答