wpf - 如何从另一个资源设置 WPF 按钮 TextBlock 样式?
问题描述
我在 WPF 应用程序中设置按钮的样式:
<Style TargetType="{x:Type Button}">
<Setter Property="ToolTipService.InitialShowDelay" Value="0"/>
<Setter Property="Height" Value="22" />
<Setter Property="Margin" Value="1" />
<Setter Property="Padding" Value="0" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
Padding="{TemplateBinding Padding}"
BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1 -1 1 0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{StaticResource NotSelectedButton}" />
<Setter Property="Foreground" Value="{StaticResource ActiveFg}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource Selected}" />
<Setter Property="Foreground" Value="{StaticResource SelectedFg}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="{StaticResource NotSelectedButton}" />
<Setter Property="Foreground" Value="{StaticResource ActiveFg}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource NotSelectedButton}" />
<Setter Property="Foreground" Value="{StaticResource InactiveFg}" />
<!--<Setter Property="Foreground" Value="{StaticResource NotSelected}" />-->
</Trigger>
</Style.Triggers>
</Style>
但是我希望在TextBlock
中使用这样定义ContentPresenter
的命名样式:TextBlock
<Style x:Key="ButtonText" TargetType="{x:Type TextBlock}">
<Setter Property="ToolTipService.InitialShowDelay" Value="0"/>
<Setter Property="Width" Value="auto" />
<Setter Property="MinWidth" Value="20" />
<Setter Property="TextAlignment" Value="Left" />
<Setter Property="Padding" Value="0 0 0 0" />
<Setter Property="Margin" Value="1 0 0 0" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
要测试这种风格,只需 a或Button
中的 a就足够了:,并且风格定义可以进入资源字典(或)。UserControl
Window
<Button>
UserControl
Window
我怎样才能做到这一点 ?
解决方案
通过直接赋值的简单解决方案
您可以通过为each分配一个TextBlock
as来直接设置样式。Content
Button
<Button>
<TextBlock Text="Test" Style="{StaticResource ButtonText}"/>
</Button>
仅支持文本作为内容
如果您确定仅将字符串作为内容放入按钮中,则可以将ContentTemplate
包含 a 的 a分配TextBlock
给ContentPresenter
. 这不适用于其他内容类型,因为它不会像往常一样显示它们,而只是它们在TextBlock
.
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1 -1 1 0">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource ButtonText}" Text="{Binding}"/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
支持所有内容类型
如果您想支持任何内容类型作为按钮的内容,您可以在ContentPresenter.Resources
. 然后,只有在是ContentPresenter
时才会应用它。对于所有其他内容类型,它将照常工作。Content
string
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1 -1 1 0">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type system:String}">
<TextBlock Style="{StaticResource ButtonText}" Text="{Binding}"/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
推荐阅读
- c - C 加密和解密中的凯撒密码,ASCII 值太高
- r - SVM 预测结果
- swift - 用函数提取数据后如何填写UItableview?
- python - 运行后的命令 cv2.imshow()、waitKey()、destroyAllWindows() 关闭镜像后继续运行
- python - 检查 gdb 内 gdb 中附加的程序的功能是否退出......并将其应用于 if 条件
- python - 测试。使用堆叠参数化装饰器时定义预期结果的最佳方法?
- javascript - 如何链接 MongoDB 集合
- google-calendar-api - Google Calendar API - 在插入 + 事件期间未发送邀请在非 Gmail 帐户中不可见
- php - 在 PHP 中组织小函数的正确方法
- python - 根据另一列的部分值创建一个热编码列