如果我使用 ControlTemplate,WPF ToggleButton 在选中时消失
WPF ToggleButton disappear when checked if I use ControlTemplate
我遗漏了一些东西,但我不明白是什么:
如果我这样做:
<ToggleButton Content="" FontFamily="Segoe MDL2 Assets" />
按钮 changes like this when pressed
但如果我使用模板更改它,它就会消失:
<ToggleButton>
<ToggleButton.Template>
<ControlTemplate>
<TextBlock Style="{StaticResource LargeIconStyle}" Text=""/>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
检查前:
检查后:
我不明白为什么。
你能帮助我吗?
勾选按钮的颜色是系统一号
样式仅供参考:
<Style
x:Key="LargeIconStyle"
BasedOn="{StaticResource BaseIconStyle}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="32" />
</Style>
<Style x:Key="BaseIconStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="TextAlignment" Value="Center" />
</Style>
问题是您使用的是 ControlTemplate
和 Template
属性。这些用于控件的 UI 和功能。因此,从本质上讲,您正在将控件变成 TextBlock
.
相反,您想使用 DataTemplate
和 ContentTemplate
属性。
我遗漏了一些东西,但我不明白是什么:
如果我这样做:
<ToggleButton Content="" FontFamily="Segoe MDL2 Assets" />
按钮
但如果我使用模板更改它,它就会消失:
<ToggleButton>
<ToggleButton.Template>
<ControlTemplate>
<TextBlock Style="{StaticResource LargeIconStyle}" Text=""/>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
检查前:
检查后:
我不明白为什么。 你能帮助我吗? 勾选按钮的颜色是系统一号
样式仅供参考:
<Style
x:Key="LargeIconStyle"
BasedOn="{StaticResource BaseIconStyle}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="32" />
</Style>
<Style x:Key="BaseIconStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="TextAlignment" Value="Center" />
</Style>
问题是您使用的是 ControlTemplate
和 Template
属性。这些用于控件的 UI 和功能。因此,从本质上讲,您正在将控件变成 TextBlock
.
相反,您想使用 DataTemplate
和 ContentTemplate
属性。