设置鼠标悬停时按钮文本的前景



尝试在Button MouseOver上做一些更改,按钮Background,BorderBrush正在按预期变化,但不是Foreground。我使用的样式,从资源字典:

<Style x:Key="RoundedCorners" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource ControlDefaultBackground}" />
<!--<Setter Property="BorderBrush" Value="{StaticResource ControlDefaultBorderBrush}" />-->
<Setter Property="Foreground" Value="Black" />
<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"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
CornerRadius="10">
<ContentPresenter x:Name="contentPresenter"
Focusable="False"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"
HorizontalAlignment="Center" 
/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ControlMouseOverBorderBrush}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="Gray" />
<Setter Property="BorderBrush" TargetName="border" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
<Setter Property="BorderBrush" TargetName="border" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource ControlBrightDefaultBackground}" />
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ControlBrightDefaultBorderBrush}" />
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

根据下面发布的建议,我不能让它在Windows.Resources之外工作,也不能让它与ControlTemplate样式一起工作(即,它只在答案的精确上下文中工作)。我的阅读让我相信关于ControlTemplate有一个层次结构,但我对模板的了解不够,无法直观地理解它

在调试器中查看前景色时,它被正确设置,但没有正确显示。

我有一个类似的问题与DataGridHeader风格,但通过添加<ContentPresenter HorizontalAlignment="Center"/>的问题被修复。不幸的是,Foreground不是ContentPresenter的一个可识别的部分,所以我对按钮的修复不是那么简单。

<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="SlateBlue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Border Background="#FF2D2D2D">
<Border BorderThickness="1"
Background="SlateBlue"
Padding="10,0,0,0"
CornerRadius="0,10,0,0">
<ContentPresenter HorizontalAlignment="Center"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>```

感谢这个答案:Button's ControlTemplate's ContentPresenter's Textblock's Foreground不改变

ContentPresenter使用的是TextBlock样式,它覆盖了Foreground的更改。添加这个到ContentPresenter通过重置TextBlock

来修复它
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{x:Null}" />
</ContentPresenter.Resources>

最新更新