2009-09-09 69 views
0

我有UserControls包含其他控件。我希望如果爲UserControl設置Foreground顏色,所有子控件都會自動繼承它。我有與字體樣式/大小相同的問題。WPF:如何向所有子控件繼承屬性值?

我可以設置這些屬性以某種方式自動/繼承嗎?這可能設置所有的子控制沒有循環?

回答

3

您可以創建資源字典來全局定義默認樣式。

您還可以引用資源字典或在任何對象中定義樣式。

在這兩種情況下,這些樣式將適用於那些沒有明確限定的風格,所有子對象......

例子:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 
    <!--Default styles that will apply to any object of the specified type (if it doesn't have style set locally)--> 
    <Style TargetType="Label" > 
     <Setter Property="FontWeight" Value="Bold"/> 
     <Setter Property="Margin" Value="0"/> 
     <Setter Property="Padding" Value="0"/> 
    </Style> 
</ResourceDictionary> 
1

查看使用樣式和BasedOn設置。

我最近寫了一個類似的例子,位於here。不幸的是,這個問題與Silver Lite有關,所以沒有回答這個問題,但我認爲它可能會給你一些想法。