首页 文章

使用背景颜色更改TextBlock前景色

提问于
浏览
0

在我的WPF应用程序中,我必须根据用户条件继续更新TextBlock背景 . TextBlock样式在App.xaml中定义 . 如果背景太暗(绿色/蓝色),我想将前景设置为白色,否则为黑色 . 我怎样才能做到这一点?我探讨了以下两个选项:

  • Via DataTriggers:在App.xaml中:
<Style TargetType="TextBlock">             
     <Setter Property="FontSize" Value="14"/>
     <Setter Property="FontStyle" Value="Normal"/>
     <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Background,PresentationTraceSources.TraceLevel=High}" Value="White">
            <Setter Property="Foreground" Value="Maroon"/>
        </DataTrigger>
     </Style.Triggers>
 </Style>

这似乎不起作用 . 我从未在textblock的foreground属性中看到更新 . 在调试时,我看到以下绑定:<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<

System.Windows.Data警告:72:RelativeSource.Self找到TextBlock(hash = 61003640)System.Windows.Data警告:78:BindingExpression(hash = 6398298):使用根项目激活TextBlock(hash = 61003640)System.Windows.Data警告:107:BindingExpression(hash = 6398298):在0级使用TextBlock.Background的缓存访问器:DependencyProperty(背景)System.Windows.Data警告:104:BindingExpression(hash = 6398298):用TextBlock替换0级的项目( hash = 61003640),使用访问器DependencyProperty(Background)System.Windows.Data警告:101:BindingExpression(hash = 6398298):使用DependencyProperty(背景)从TextBlock(hash = 61003640)获取0级的GetValue:SolidColorBrush(hash = 58614288) System.Windows.Data警告:80:BindingExpression(hash = 6398298):TransferValue - 得到原始值SolidColorBrush(hash = 58614288)System.Windows.Data警告:89:BindingExpression(hash = 6398298):TransferValue - 使用最终值SolidColorBrush( hash = 58614288)<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <

什么是“SolidColorBrush(hash = 58614288)”?是HexC颜色代码还是SolidColorBrush类型对象的代码?

  • 使用IValueConverter:没有尝试过,因为我根据其他一些属性更改而没有't want to convert one value to another but change a UIElement'属性 . 此外,由于几乎所有UIElements都在内部使用TextBlock来显示数据,因此转换器不会产生性能影响吗?

我已经看过以下帖子:Change TextBlock foreground color based on the background . 它对我的情况没有帮助 . 任何帮助都非常感谢 .

谢谢,

RDV

关于我的应用程序的更多信息:

当我的应用程序启动时,我的TextBlocks具有默认的背景颜色 . 所有Textblock样式都存储在ResourceDictionary中,该ResourceDictionary存储在不同的解决方案中 . 我的应用程序的App.xaml中只有一个ResourceDictionary:

<Application x:Class="MySolution"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ResourcesSolution;component/Resources/GenericStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

从这里正确地拾取FontWeight,FontStyle,甚至前景等 . 但这些都是静态属性 . 在某些用户操作中,我在运行时更改TextBlock的背景颜色,但有时会使文本无法读取,如绿色背景上的黑色文本 . 当背景颜色发生变化时,我也可以绑定前景色,但在这种情况下,我必须在所有视图中进行绑定 . 相反,我希望有一个全局风格来处理这项工作,这样即使我忘记绑定前景色,也会自动选择正确的颜色 .

我有一个很大的应用程序,性能是一个主要问题 . 这就是为什么我对使用转换器犹豫不决并且正在寻找一些基于xaml的解决方案,因为这只是一个基于条件的问题 .

1 回答

  • 1

    我通过在TextBlock控件上设置Background来测试我的代码,我可以看到以下样式触发器在声明为全局样式表时按预期工作:

    <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Style.Triggers>
                <Trigger Property="Background" Value="White">
                    <Setter Property="Foreground" Value="Aqua"/>
                </Trigger>
                <Trigger Property="Background.Color" Value="Transparent">
                    <Setter Property="Foreground" Value="BlueViolet"/>
                </Trigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Background}" Value="White">
                    <Setter Property="Foreground" Value="Maroon"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Background.Color}" Value="#FF008000">
                    <Setter Property="Foreground" Value="Blue"/>
                </DataTrigger>
    
            </Style.Triggers>
        </Style>
    

    但是,我最初没有注意到这种行为,因为我认为Button的内容(由TextBlock内部表示)也应该使用TextBlock的样式触发器(它正在拾取TextBlock的FontSize和FontStyle以全局样式定义) .

    我认为这与ContentPresenter问题有关,应该在另一个线程中解决 .

    谢谢,

    RDV

相关问题