WPF如何改变GridView字体大小
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF如何改变GridView字体大小相关的知识,希望对你有一定的参考价值。
参考技术A 试试增加属性<Gridview ....... TextBlock.FontSize="36".......或者
<GRidView.resources>
<style targettype="TextBlock">
<setter property="TextBlock.FontSize" value="36"/>
</style>
</GRidView.resources>本回答被提问者采纳 参考技术B 模板
如何在 WPF C# 中更改验证结果的字体大小
【中文标题】如何在 WPF C# 中更改验证结果的字体大小【英文标题】:How to change font size of validation result in WPF C# 【发布时间】:2017-05-05 07:11:54 【问题描述】:我有 ValidationRule 使用 System.Windows.Controls 并且该规则返回一些自定义消息。但是字体太小了,怎么修改字体大小呢?
截图...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>
【问题讨论】:
【参考方案1】:我遇到了这篇文章,想让人们知道在 XAML 中的 Material Design 的当前版本中,现在有一个提示帮助:
<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>
【讨论】:
这就像一个魅力。您是如何发现这一点的,以便其他人可以通过类似的功能更新帮助自己? 老实说,我不记得了 - 我相信我正在查看他们的 github 并发现字体大小问题被记录为问题。由于 SO 不鼓励外部链接,我举了一个例子!【参考方案2】:根据您的要求,您需要修改Validation.ErrorTemplate
。由于您使用的是 MeterialDesign,您可以根据现有样式为 TextBox
编写修改后的样式,或者仅编写修改后的 ErrorTemplate 并使用 Validation.ErrorTemplate
属性将其直接应用于 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>
TextBox ErrorTemplate 的样式复制自here
【讨论】:
你能解释一下如何将ControlTemplate
绑定到文本框吗?因为我看不出它们之间有任何联系,而且发生了很多事情。我真的很感激。
@FCin 请查看已发布代码中的 ExtendedMaterialDesignTextBox 样式。将 ControlTemplate(ModifiedErrorTemplate) 应用于 TextBox 的 Validation.ErrorTemplate 属性。
@FCin, @WPFUser 我认为它只是 ControlTemplate
的不匹配键名。 MaterialDesignValidationErrorTemplate 用于声明,ModifiedErrorTemplate 用于使用。
好的,我现在看到了。这是我昨天一直在寻找的,今天我看到了这篇文章:) 谢谢。以上是关于WPF如何改变GridView字体大小的主要内容,如果未能解决你的问题,请参考以下文章
如何计算已知字体大小和字符的 WPF TextBlock 宽度?