这是我第一次使用资源字典来设置我的UI控件的样式。好吧,我有一个问题,在按钮中显示文本,也许问题是显示按钮我不知道什么是错的。我向App.xalm添加了资源字典。在设计视图中,它看起来都很好 - 所有都没有Text in按钮。
我试过了:
资源字典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CyclingTrainingPlan">
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="#b8c7ce" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Height" Value="Auto" />
<Setter Property="Width" Value="Auto" />
<Setter Property="Margin" Value="20" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
XALM:
<Button Style="{StaticResource buttonStyle}" Content="Dashboard" />
解:
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<!-- Properties... -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#1a2226"/>
</Trigger>
</Style.Triggers>
我在这里测试过,它按预期工作得很好。
也许问题可能在于这种风格的关联。尝试从<Button Style="{StaticResource buttonStyle}" Content="Dashboard" />
更改为<Button Style="{DynamicResource buttonStyle}" Content="Dashboard" />