我试过以下......
我错过了什么!?我假设我做了一些小的疏忽。 (这不是我的代码,但现在我的责任是:\
另外,请注意这个事实,使用此样式的位置,按钮的命令和内容绑定到mvvm样式视图模型。
<Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" SnapsToDevicePixels="true">
<Border.Background>
<SolidColorBrush Color="{StaticResource Color2}"/>
</Border.Background>
<ContentPresenter x:Name="contentPresenter" Focusable="False" TextElement.Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" TargetName="border" Value="{StaticResource ButtonIsFocusedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsDefaulted" Value="true"/>
<!---HERE IS THE PROBLEM (note: the background works fine)--->
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource ButtonHoverOverBackgroundBrush}"/>
<Setter Property="TextBlock.Foreground" TargetName="contentPresenter" Value="White"/>
</Trigger>
<!---HERE IS THE PROBLEM--->
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="#FF8B7A54"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.33"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:3)
好的,我终于明白了。我睡着了,意识到我的应用程序中的其他地方定义了默认的文本块样式。它已应用于contentpresenter textblock属性。不知何故(我不确定如果有人想对此发表评论)这种风格阻止了触发器正常工作。所以这是解决方案 - &gt;我只发布已编辑的内容演示者...
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding Content}"
TextBlock.Foreground="{TemplateBinding Foreground}"
TextElement.Foreground="{TemplateBinding Foreground}"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{x:Null}" />
</ContentPresenter.Resources>
</ContentPresenter>
通过清除文本块样式,触发器可以正常工作。我想知道为什么会这样。