我尝试了以下...
- 设置按钮的 TextBlock.Foreground。
- 设置 contentpresenter 的 TextBlock.Foreground。
- 设置 IsMouseOver 触发器,如下所示。
- 设置没有目标名称的 IsMouseOver 触发器(假设它点击了按钮)。
- 我尝试过 TextBlock.Foreground 的所有地方,我都尝试过 TextElement.Foreground。
我错过了什么!?我假设我做了一些小疏忽。 (这不是我的代码,但现在是我的责任:\
另外,请注意这个事实,使用此样式的地方、按钮的命令和内容都绑定(bind)到 mvvm 样式 View 模型。
<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>
最佳答案
好吧,我终于明白了。我睡了一觉,意识到我在我的应用程序的其他地方定义了一个默认的文本 block 样式。它被应用于 contentpresenter 文本 block 属性。不知何故(我不确定如果有人想对此发表评论)这种风格阻止了触发器正常工作。所以这是解决方案 -> 我只发布编辑后的内容演示者......
<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>
通过清除文本 block 样式,触发器可以工作。我想知道为什么会这样。
关于c# - Button 的 ControlTemplate 的 ContentPresenter 的 Textblock 的前景没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115169/