本文介绍了Button 的 ControlTemplate 的 ContentPresenter 的 Textblock 的 Foreground 没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下...

  1. 设置按钮的 TextBlock.Foreground.
  2. 设置 contentpresenter 的 TextBlock.Foreground.
  3. 设置 IsMouseOver 触发器,如下所示.
  4. 在没有目标名称的情况下设置 IsMouseOver 触发器(假设它点击了按钮).
  5. 在我尝试过 TextBlock.Foreground 的所有地方,我都尝试过 TextElement.Foreground.

我错过了什么!?我假设我做了一些小疏忽.(这不是我的代码,但现在是我的责任:\

What am I missing!? I am assuming there is some minor oversight I have made. (It is not my code, but it is now my responsibility :\

另外,请注意这个事实,使用此样式的地方、按钮的命令和内容都绑定到 mvvm 样式视图模型.

Also, be aware of this fact, the places where this style is used, the button's command and content are bound to a mvvm style view model.

    <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>

推荐答案

好吧,我终于想通了.我睡在它上面并意识到我在我的应用程序的其他地方定义了一个默认的文本块样式.它被应用于 contentpresenter textblock 属性.不知何故(我不确定是否有人想对此发表评论)这种风格阻止了触发器正常工作.所以这是解决方案 -> 我只发布编辑过的内容演示者...

Ok, I finally figured it out. I slept on it and realized that I have a default textblock style defined elsewhere in my application. It was getting applied to the contentpresenter textblock property. Somehow (I am not sure how if someone wants to comment on it) this style was blocking the trigger from working properly. So here is the solution -> I am only posting the edited content presenter...

                    <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>

通过清除文本块样式,触发器工作.我想知道为什么会这样.

By clearing the textblock style, the trigger works. I would like to know why this is though.

这篇关于Button 的 ControlTemplate 的 ContentPresenter 的 Textblock 的 Foreground 没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:30