2012-02-08 103 views
4

我想在Window.Resources中創建多個樣式。下面是我試過的代碼,但它不工作:如何在WPF Window.Resources中設置樣式。

<Window.Resources> 
    <Style x:Key="StyleOne" TargetType="{x:Type Control}"> 
     <Setter Property="Control.Background" Value="Blue"></Setter> 
     <Setter Property="Control.Height" Value="20"></Setter> 
    </Style> 
    <Style x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}"> 
     <Setter Property="Control.Background" Value="Red"></Setter> 
     <Setter Property="Control.Height" Value="20"></Setter> 
    </Style> 
</Window.Resources> 
<Button Style="{StaticResource StyleOne}"></Button> 
<Button Style="{StaticResource StyleTwo}"></Button> 

它拋出一個錯誤說:

屬性「內容」設置不止一次。

回答

6

此錯誤與樣式無關,該窗口只能包含一個孩子(其設置了Content),請使用某個容器可以包含多個孩子。例如一個StackPanelGrid

<StackPanel> 
    <Button .../> 
    <Button .../> 
</StackPanel> 

(參見:Panels Overview

+0

我想這wasnt問題,因爲樣式元素僅由window.resource並沒有其他的支持,我想的StackPanel和電網還可以,問題是裏面我的第二種風格的x:類型控制,由@Kishore Kumar解決,反正謝謝,因爲給你的時間 – Abbas 2012-02-10 17:43:35

-2

我想支持算法FMP繼承了其他風格類型的屬性,你必須

Property="Control.Background" 

兩個樣式集,因此得到一個錯誤

"The property "Content" is set more than once." 
+0

'Background'!='Content' – 2012-02-09 00:11:54

4

設置第二種風格的目標類型

<Style x:Key="StyleTwo" 
      BasedOn="{StaticResource StyleOne}" 
      TargetType="{x:Type Control}"> 
     <Setter Property="Control.Background" 
       Value="Red"></Setter> 
     <Setter Property="Control.Height" 
       Value="20"></Setter> 
    </Style> 

把按鈕一個StackPanel或網格

+0

謝謝,那解決了我的問題 – Abbas 2012-02-10 17:43:49

+1

如果它幫助你解決請標記爲回答 – 2012-02-10 18:25:36

相關問題