【发布时间】:2023-04-03 05:03:01
【问题描述】:
我有一个基本风格,比如说:
<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource DefaultButtonBackground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="ButtonShapeGrid" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">
<!-- snip some shaping Xaml. -->
<ContentPresenter
Grid.RowSpan="2"
VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在,我需要创建一些样式,用固定的东西替换按钮的内容......就像这样(其中 CloseButtonGlyph 是路径资源)。这工作正常:
<Style x:Key="CloseButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
<Setter Property="Content" Value="{StaticResource CloseButtonGlyph}" />
</Style>
但是,我还需要在内容中添加任意 Xaml,这就是我遇到问题的地方。在 WPF 窗口或控件中使用样式时,以下内容不起作用(我收到一个设计时错误,指出内容无法从 ContentElement 或 Visual 派生):
<Style x:Key="WingDingStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
<Setter Property="Content">
<Setter.Value>
<TextBlock FontFamily="WebDings" Text="3" FontSize="18" Foreground="White" />
</Setter.Value>
</Setter>
</Style>
我不知道我需要做什么。将 TextBox 放在窗口或控件的按钮内容中效果很好,为什么不放在样式中?
【问题讨论】: