2013-05-29 66 views
4

如何獲取xaml中樣式設置器屬性的值?從xaml中的樣式設置器屬性獲取值

例如,我有一個樣式:

<Style TargetType="TextBox"> 
    <Setter Property="Background" Value="YellowGreen" /> 
</Style> 

我怎樣才能得到背景屬性從文字框默認樣式的價值?

<Style TargetType="Button"> 
    <Setter Property="Background" Value="{Binding ???}" /> 
</Style> 

我需要這個,因爲我沒有訪問TextBox風格 ..

回答

5

如果你不能修改文本框樣式,你可以做這項工作,各地(測試工作):

<TextBox x:Key="DefaultTextBox" /> 
<Style TargetType="Button"> 
    <Setter Property="Background" 
    Value="{Binding Source={StaticResource DefaultTextBox}, Path=Background}" /> 
</Style> 

你不能在XAML綁定到風格的setter背景。

+0

它的工作原理!非常感謝! – Chepene

3

你應該重構你的XAML:

<SolidColorBrush x:Key="BackgroundBrush" Color="YellowGreen" /> 
<Style TargetType="TextBox"> 
    <Setter Property="Background" Value="{StaticResource BackgroundBrush}" /> 
</Style> 

<Style TargetType="Button"> 
    <Setter Property="Background" Value="{StaticResource BackgroundBrush}" /> 
</Style> 

綁定妨礙性能,並不意味着這種的行動。

+0

我無法更改TextBox樣式,我無法訪問它。所以我不能用你的方法... – Chepene