c# - WPF 在触发条件下无法将 "Red"的静态资源识别为红色画笔

标签 c# wpf xaml datatrigger staticresource

我得到了这个 TextBlock:

<TextBlock Foreground="Red"/>

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

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

这个触发条件不成立! 如果静态资源在加载时解析,那么为什么这个触发器会失败? XAML 加载程序不应该在触发条件中将红色放在那里吗?还是用一些表达代替? 是否有可能因为触发条件的“值”属性不是依赖属性而发生这种情况?

只有在我写的时候

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

有效。

如果我从外部放置静态资源(如下所示),它在任何情况下都不起作用。像那样:

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

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

最佳答案

确保,您测试的 TextBlocks 和 StyleTrigger 使用(!)相同的笔刷红色或来自 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>

关于c# - WPF 在触发条件下无法将 "Red"的静态资源识别为红色画笔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48443777/

相关文章:

wpf - MVVM 绑定(bind)属性和子属性

c# - ComboBox 中的默认加载值 - WPF MVVM

wpf - 要构建像 WPF 这样的框架,我应该知道什么?

c# - 具有参数的WPF Mvvm导航

wpf - 如何修复键属性只能用于IDictionary中包含的元素

.net - 是否可以在 xaml winrt 应用程序中创建自定义主题画笔?

C# implicit cast "overloading"和反射问题

c# - 从通用接口(interface)继承

c# - 有没有办法唤醒 sleep 线程?

c# - 我可以在 WinRT 中更改主磁贴的背景颜色吗?