【发布时间】:2017-03-27 16:02:28
【问题描述】:
我有以下 XAML 代码,它创建一个带有单个按钮的窗口。当我单击按钮时,前景色变为红色,但背景色没有变化。为什么一个变了,另一个没有变?
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfExperiments"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,0,2">
<Grid.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Button Style="{StaticResource ButtonStyle}"
Content="Button"
HorizontalAlignment="Left"
Margin="135,97,0,0"
VerticalAlignment="Top"
Width="75"
/>
</Grid>
</Window>
当然,我首先在 Google 上搜索过。我发现的大部分内容都说解决方法是使用模板而不是样式,如果这是解决方案,我当然愿意这样做。但我在one SO answer 中注意到按钮具有覆盖样式值的默认模板。如果是这样,为什么我的 XAML 会影响前台?
【问题讨论】: