【发布时间】:2014-12-25 01:26:11
【问题描述】:
使用过 FXML 并看到 FXML CSS 样式和 XAML ResourceDictionary 样式之间的相似之处。
使用 FXML,我能够设置按钮标签的样式,使其具有内阴影效果。
我正在尝试对 XAML 中的按钮做同样的事情。
我不需要确切的代码来实现样式,我只需要知道该代码在资源字典的标记中的位置。
如何设置 XAML 按钮文本的样式?
编辑
我要感谢 McGarnagle 让我朝着正确的方向前进: 这是我能想到的(感谢我从here 得到的一点)
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Grid.Resources>
<TextBlock Foreground="{DynamicResource{x:Static SystemColors.GrayTextBrushKey}}" Text="{TemplateBinding Content}">
<TextBlock.RenderTransform>
<TranslateTransform X="-2" Y="-2" />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
WPF 中没有“按钮的标签”之类的东西。 Java 的东西实际上是对 .Net 提供的设计不佳的模仿,就像 Java 中的其他所有东西一样。阅读 WPF Content Model 和 Visual Tree 以更好地了解 WPF UI 元素的组成方式。
标签: wpf xaml resourcedictionary