2011-06-02 57 views
2

我需要將樣式應用於堆棧面板中的不同控件。它們都是不同的類型,即TreeView,ListView,ComboBox等。 有沒有一種方法可以將StackPanel級別的樣式應用於這些控件。 我不想將風格單獨應用於這些控件。 有什麼辦法可以做到這一點?WPF-如何將樣式應用於面板中的多個控件

謝謝..

回答

3

可以通過聲明樣式withing的StackPanel的資源不喜歡這樣。你必須聲明每個Style沒有一個鍵,以便它們被自動應用到StackPanel中的每個目標控件。

<StackPanel> 
    <StackPanel.Resources> 
     <!-- Styles declared here will be scoped to the content of the stackpanel --> 

     <!-- This is the example of style declared without a key, it will be applied to every TreeView. Of course you'll have to add Setters etc --> 
     <Style TargetType="TreeView"> 
     </Style> 
    </StackPanel.Resources> 

    <!-- Content --> 

    <!-- This treeview will have the style declared within the StackPanel Resources applied to it--> 
    <TreeView /> 
</StackPanel> 
+0

在一些問題根據需要與所有這些控件的公共基類的TargetType指定,在標籤上你可能想要手動應用語法突出顯示,請參閱我編輯的代碼或[編輯幫助](http://stackoverflow.com/editing-help)中的*「Stack Exchange additions」*部分。 – 2011-06-02 22:09:51

+0

@ H.B。 :o我不知道這些,謝謝你的提示;) – 2011-06-03 10:39:14

0

正如讓 - 路易說,你可以指定StackPanel資源字典內的Style,這將只適用於匹配StackPanel中的元素。

爲了有一個Style匹配所有控件,你會如Control

<StackPanel> 
    <StackPanel.Resources> 
    <Style TargetType="Control"> 
     <!-- Setters etc here --> 
    </Style> 
    </StackPanel.Resources> 

<!-- Controls here --> 

</StackPanel> 
相關問題