2012-02-03 62 views
0

我在我的WPF應用程序,我想用類似取消自定義樣式中的值?

<Window Foreground="Red"> 
    <TextBlock Text="Hello World" /> 
</Window> 

這工作正常的TextBlocks,但如果我添加一個按鈕,字體也一直是黑色,因爲在主窗口中設置前景色改變文字顏色按鈕的默認樣式中有前景設置器。我可以在原有基礎上的,但除去Foregound制定者巴頓一個新的默認風格?

回答

2

我沒有測試過這一點,但你可以嘗試適當的資源集合中使用基本樣式(例如,在App.xaml中的應用水平),然後創建一個隱式樣式,基於此每種類型的控制基地風格。

<Style TargetType="{x:Type Control}" x:Key="DefaultControlStyle"> 
    <Setter Property="Foreground" Value="Red" /> 
</Style> 
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultControlStyle}" /> 
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource DefaultControlStyle}" />   
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultControlStyle}" /> 
<Style TargetType="{x:Type ListView}" BasedOn="{StaticResource DefaultControlStyle}" /> 

你也可以使用綁定招描述here相對源。

+0

這種方法使得基於觸發器更改某些UI部件的前景色更困難一點(例如,您有一個ContentPresenter應該代表某個錯誤,包含多個子元素,並且希望將Foreground設置爲Red整個內容基於ContentPresenters父級的觸發器)。 – Jens 2012-02-03 09:21:41

+0

我剛剛在這裏找到了一些類似的方法 - 我不知道這些是否是可取的 - http://stackoverflow.com/questions/5327819/wpf-setting-foreground-color-of-entire-form – devdigital 2012-02-03 09:38:09

+0

Pavlo格拉茲科夫的回答非常好,謝謝!你要編輯你的答案,這樣我可以接受它,或者我應該張貼我自己? – Jens 2012-02-03 10:23:23