2013-08-28 70 views
0

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

  1. 通过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(散列= 61003640) System.Windows。数据警告:78:BindingExpression(hash = 6398298):用根项目激活TextBlock(hash = 61003640) System.Windows.Data警告:107:绑定表达式(哈希= 6398298):在级别0使用TextBlock.Background的缓存访问器:DependencyProperty(Background) 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 Warning:80:BindingExpression(hash = 6398298):TransferValue - 得到原始值SolidColorBrush(hash = 58614288) System.Windows。数据警告:89:BindingExpression(散列= 6398298):TransferValue - 使用最终值的SolidColorBrush(散列= 58614288) < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < <

什么是 “的SolidColorBrush(哈希值= 58614288)”?它是十六进制颜色代码还是用于SolidColorBrush类型对象的hascode?

  1. 使用IValueConverter:还没有尝试过,因为我不想将一个值转换为另一个值,而是根据其他属性更改来更改UIElement的属性。另外,由于几乎所有的UIElements都在内部使用TextBlock来显示数据,转换器是否会给性能造成影响?

我已经看过以下线程:Change TextBlock foreground color based on the background。它没有帮助我的情况。 任何帮助是高度赞赏。

感谢,

RDV

,稍微介绍一下我的应用程序:

当我的应用程序启动时,我的TextBlocks有默认的背景色。所有的Textblock样式都存储在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的解决方案,因为这只是一个基于条件的问题。

+0

的SolidColorBrush是可以适用于背景单一颜色刷前景/等你能告诉你的XAML的风格。 –

+0

我也尝试绑定到Background.Color。 – RDV

回答

1

我的只有TextBlock控件设置背景测试我的代码,我可以看到作为全局样式表声明时以下样式触发按预期工作:

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

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

我认为这与ContentPresenter问题有关,应该在不同的线程中解决。

感谢,

RDV

+0

http://stackoverflow.com/questions/18541906/change-foreground-color-dynamically-based-on-background-color – RDV