【问题标题】:WPF isn't recognize static resource of "Red" as Red brush in trigger conditionWPF 在触发条件下无法将“红色”的静态资源识别为红色画笔
【发布时间】:2018-07-04 18:07:51
【问题描述】:

我得到了这个文本块:

<TextBlock Foreground="Red"/>

TextBlock 有一个隐式样式,带有样式触发器 询问“如果前景是 {StaticResource BrushTextBlockAlertForeground} 然后将背景设置为黑色。” (BrushTextBlockAlertForeground 是红色的,当然)。

<Trigger Property="Foreground" Value="{StaticResource BrushTextBlockAlertForeground}">
    <Setter Property="Background" Value="Black"/>
</Trigger>

此触发条件失败! 如果静态资源在加载时解决,那么为什么这个触发器会失败? XAML 加载器不应该在触发条件中放置 Red 吗?或者它提出了一些表达? 是否有可能因为触发条件的“值”属性不是依赖属性而发生?

只有当我写的时候

<Trigger Property="Foreground" Value="Red">
    <Setter Property="Background" Value="Black"/>
</Trigger>

有效。

如果我从外部放置静态资源(如下所示),无论如何它都不起作用。像这样:

<TextBlock Foreground="{StaticResource BrushTextBlockAlertForeground}"/>

我很想知道背后的原因,因为我想写出可重复使用的颜色,而不是在很多地方加上“红色”。 “明天”有人会尝试使其可重用,并会遇到我遇到的行为。

【问题讨论】:

  • 无法重现该问题。它没有显示在我的设计器中,但是当我运行应用程序时,Background 更改为 Black
  • 如果文本框和触发器中的值是相同的类型,你会工作
  • @Rise 这还不够,它们来自同一个类型,但它们应该是同一个对象。

标签: c# wpf xaml datatrigger staticresource


【解决方案1】:

确保您测试的 TextBlocks 和 StyleTrigger 都使用(!)相同的笔刷 Red 或来自 StaticResource。具有前景红色的 TextBlock 和具有 StaticResource 的 StyleTrigger(反之亦然)将不起作用,因为 Brushes.Red 和来自 StaticResource 的值不相等。见A: How to Compare SolidColorBrushes?

<StackPanel>
            <!--this doesn't work-->
            <StackPanel.Resources>
                <SolidColorBrush x:Key="forebr" Color="Red"/>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="{StaticResource forebr}">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="Red" Text=" Test trigger 0"></TextBlock>
        </StackPanel>

        <StackPanel>
            <!--this doesn't work-->
            <StackPanel.Resources>
                <SolidColorBrush x:Key="forebr" Color="Red"/>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="Red">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="{StaticResource forebr}" Text=" Test trigger 1"></TextBlock>
        </StackPanel>

        <StackPanel>
            <!--this works-->
            <StackPanel.Resources>
                <SolidColorBrush x:Key="forebr" Color="Red"/>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="{StaticResource forebr}">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="{StaticResource forebr}" Text=" Test trigger 2"></TextBlock>
        </StackPanel>

        <StackPanel>
            <!--this works-->
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="Red">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="Red" Text=" Test trigger 3"></TextBlock>
        </StackPanel>

【讨论】:

    猜你喜欢
    • 2018-03-19
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多