如何在 WPF C# 中更改验证结果的字体大小

How to change font size of validation result in WPF C#

我通过使用 System.Windows.Controls 和该规则 return 一些自定义消息来获得 ValidationRule。但是字体太小了,如何修改字体大小?

截图...Textbox content and error content of validation result

这是 WPF 的示例代码 Window App...

public class NotEmptyValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if(value != null)
            return string.IsNullOrWhiteSpace((value ?? "").ToString())
                ? new ValidationResult(false, "Field is required")
                : ValidationResult.ValidResult;
        return ValidationResult.ValidResult;
    }
}

这里是 XAML...(注意:在 TextBox 标签处更改字体大小无效)

<Page...

<TextBox 
        x:Name="txtName" 
        Grid.Row="1" 
        Grid.Column="1" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Width="210" 
        Margin="5,0,0,0"
        FontSize="14"
        Style="{StaticResource MaterialDesignTextBox}">
    <TextBox.Text>
        <Binding Path="RCName" UpdateSourceTrigger="PropertyChanged">
           <Binding.ValidationRules>
                <local2:NotEmptyValidationRule ValidatesOnTargetUpdated="True"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

</Page>

根据您的要求,您需要修改Validation.ErrorTemplate。由于您使用的是 MeterialDesign,因此您可以根据现有样式为 TextBox 编写修改后的样式,或者仅编写修改后的 ErrorTemplate 并使用 Validation.ErrorTemplate 属性.[=15 将其直接应用于 TextBox =]

    <Grid xmlns:controlzEx="clr-namespace:ControlzEx" xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
    <Grid.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <!--  New Validation ErrorTemplate  -->
        <ControlTemplate x:Key="ModifiedErrorTemplate">
            <ControlTemplate.Resources>
                <DataTemplate DataType="{x:Type ValidationError}">
                    <TextBlock MaxWidth="{Binding ElementName=Placeholder,
                                                  Path=ActualWidth}"
                               Margin="2"
                               HorizontalAlignment="Left"
                               FontSize="12"
                               Foreground="{DynamicResource ValidationErrorBrush}"
                               Text="{Binding ErrorContent}"
                               TextWrapping="Wrap"
                               UseLayoutRounding="false" />
                </DataTemplate>
            </ControlTemplate.Resources>
            <StackPanel>
                <AdornedElementPlaceholder Name="Placeholder" />
                <Border Name="DefaultErrorViewer"
                        Background="{DynamicResource MaterialDesignPaper}"
                        Visibility="Collapsed">
                    <TextBlock MaxWidth="{Binding ElementName=Placeholder,
                                                  Path=ActualWidth}"
                               Margin="0 2"
                               HorizontalAlignment="Left"
                               FontSize="10"
                               Foreground="{DynamicResource ValidationErrorBrush}"
                               Text="{Binding CurrentItem.ErrorContent}"
                               TextWrapping="Wrap"
                               UseLayoutRounding="false" />
                </Border>
                <controlzEx:PopupEx x:Name="ValidationPopup"
                                    AllowsTransparency="True"
                                    IsOpen="False"
                                    Placement="Bottom"
                                    PlacementTarget="{Binding ElementName=Placeholder,
                                                              Mode=OneWay}">
                    <Border Background="{DynamicResource MaterialDesignPaper}">
                        <TextBlock Margin="0 2"
                                   HorizontalAlignment="Left"
                                   FontSize="10"
                                   Foreground="{DynamicResource ValidationErrorBrush}"
                                   Text="{Binding CurrentItem.ErrorContent}"
                                   TextWrapping="Wrap"
                                   UseLayoutRounding="false" />
                    </Border>
                </controlzEx:PopupEx>
            </StackPanel>
            <ControlTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False" />
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True" />
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False" />
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False" />
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="DefaultErrorViewer" Property="Visibility" Value="Visible" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="True" />
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="ValidationPopup" Property="IsOpen" Value="{Binding ElementName=Placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="True" />
                        <Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="DefaultErrorViewer" Property="Visibility" Value="{Binding ElementName=Placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <!--  Derived TextBox Style  -->
        <Style x:Key="ExtendedMaterialDesignTextBox"
               BasedOn="{StaticResource MaterialDesignTextBox}"
               TargetType="TextBox">
            <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ModifiedErrorTemplate}" />
        </Style>
    </Grid.Resources>
    <TextBox x:Name="txtName"
             Grid.Row="1"
             Grid.Column="1"
             Width="210" Style="{StaticResource ExtendedMaterialDesignTextBox}"
             Margin="5,0,0,0"
             HorizontalAlignment="Left"
             VerticalAlignment="Top"
             FontSize="14">
        <TextBox.Text>
            <Binding NotifyOnValidationError="True"
                     Path="RCName"
                     UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
</Grid>

已从 here

复制 TextBox ErrorTemplate 的样式

我运行进入这个post,想让大家知道在Material设计的当前版本XAML中,现在有一个提示辅助为此:

            <TextBox
                x:Name="Foo"
                materialDesign:ValidationAssist.FontSize="12"
                Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>

或者您可以将其设置为适用于所有元素的样式

<Style
    x:Key="MaterialDesignFloatingHintTextBox"
    BasedOn="{StaticResource MaterialDesignFloatingHintTextBox}"
    TargetType="{x:Type TextBox}">
    <Setter Property="materialDesign:ValidationAssist.FontSize" Value="12" />
</Style>