在XAML中使用ControlTemplate时按钮内容消失
本文关键字:按钮 消失 ControlTemplate XAML | 更新日期: 2023-09-27 18:29:12
我为表单编写了一个样式,除了按钮上没有文本外,一切似乎都很好。我得到的只是一个没有内容的灰色按钮。
以下是我尝试过的:
<Style x:Key="CodeButton" TargetType="{x:Type Button}" >
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Content" Value="Enter"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
Name="Border"
CornerRadius="4"
Padding="10"
BorderBrush="#666666"
Background="#e5e5e5"
BorderThickness="1">
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="#90c74b"/>
<Setter TargetName="Border" Property="BitmapEffect">
<Setter.Value>
<DropShadowBitmapEffect Color="#90c74b" Direction="0" ShadowDepth="4" Opacity="0.7" Softness="0.8" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
正如您在第6行看到的,我希望按钮的内容是"Enter"。
您需要在模板中有一个ContentPresenter
才能显示该文本。
试试这个:
<Style x:Key="CodeButton" TargetType="{x:Type Button}" >
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Content" Value="Enter"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
Name="Border"
CornerRadius="4"
Padding="10"
BorderBrush="#666666"
Background="#e5e5e5"
BorderThickness="1">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="#90c74b"/>
<Setter TargetName="Border" Property="BitmapEffect">
<Setter.Value>
<DropShadowBitmapEffect Color="#90c74b" Direction="0" ShadowDepth="4" Opacity="0.7" Softness="0.8" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>