2013-08-28 59 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