c# - Button 的 ControlTemplate 的 ContentPresenter 的 Textblock 的前景没有改变

标签 c# wpf xaml styles

我尝试了以下...

  1. 设置按钮的 TextBlock.Foreground。
  2. 设置 contentpresenter 的 TextBlock.Foreground。
  3. 设置 IsMouseOver 触发器,如下所示。
  4. 设置没有目标名称的 IsMouseOver 触发器(假设它点击了按钮)。
  5. 我尝试过 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/

相关文章:

c# - 从 C# 代码隐藏访问 XAML 控件

c# - 使用 EPPlus 对列的组合框范围进行数据验证

c# - 如何在 MahApps.Metro 中添加 AnimatedTabControl?

c# - 我的 C# Windows 服务无法写入文件,同时另一个 C# 应用程序正在尝试读取该文件

c# - Wpf DataGrid 添加新行

使用 MediaElement 的 WPF 动画 GIF

c# - 为什么我的转换器给出无效的转换错误?

c# - 如何像网格一样拆分堆栈面板

c# - 如何在可选扩展名之前将随机字符串附加到文件名?

c# - 从第二个列表中获取唯一元素