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

aydmsdu9  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(163)

尝试在Button MouseOver上做一些更改,按钮BackgroundBorderBrush按预期更改,但不是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是不是一个recognised部分的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>```
41ik7eoe

41ik7eoe1#

感谢这个答案:Button's ControlTemplate's ContentPresenter's Textblock's Foreground not changing
ContentPresenter正在使用TextBlock样式,该样式覆盖了Foreground的更改。将其添加到ContentPresenter通过重置TextBlock来修复它

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

相关问题