2012-02-08 61 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

相关问题