按钮的ControlTemplate的ContentPresenter的Textblock的Foreground没有改变。

3
我尝试了以下几种方法:
  1. 设置按钮的TextBlock.Foreground。
  2. 设置contentpresenter的TextBlock.Foreground。
  3. 按照此处所示,设置IsMouseOver Trigger。
  4. 没有指定目标名称(假设它会命中按钮),设置IsMouseOver Trigger。
  5. 在所有尝试使用TextBlock.Foreground的地方,我都尝试了TextElement.Foreground。
我错过了什么?我认为我可能犯了一些小错误。(这不是我的代码,但现在是我的责任:\
还要注意的是,使用此样式的位置,按钮的命令和内容都绑定到mvvm样式视图模型。
    <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>
1个回答

9

好的,我终于弄清楚了。我睡了一晚上,意识到我在应用程序中定义了默认的文本块样式。它被应用到contentpresenter文本块属性上。不知何故(如果有人想评论一下,我不确定是怎么回事),这个样式阻止了触发器正常工作。因此,这就是解决方案->我只发布编辑后的内容呈现器...

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

清除文本块样式后,触发器可以正常工作。但我想知道为什么会这样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接