2010-09-18 76 views
1

我有一个像WPF:样式不适用

<ribbon:RibbonGallery> 
    <ribbon:RibbonGallery.Resources> 
     <Style TargetType="ribbon:RibbonGalleryItem"> 
      <Setter Property="Width" Value="24" /> 
      <Setter Property="Padding" Value="0" /> 
     </Style> 
     <Style TargetType="Rectangle"> 
      <Setter Property="Width" Value="16" /> 
      <Setter Property="Height" Value="16" /> 
     </Style> 
    </ribbon:RibbonGallery.Resources> 

    </ribbon:RibbonGalleryCategory> 
    <ribbon:RibbonGalleryCategory x:Name="themeColors" Header="Theme Colors" MinColumnCount="10" MaxColumnCount="10"> 
     <ribbon:RibbonGalleryCategory.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal" > 
        <Rectangle Fill="{Binding}" /> 
       </StackPanel> 
      </DataTemplate> 
     </ribbon:RibbonGalleryCategory.ItemTemplate> 
    </ribbon:RibbonGalleryCategory> 
</ribbon:RibbonGallery> 

我的宽度和高度的设置不会应用到矩形。我想知道什么是错

回答

2

你需要给你的风格Key然后引用您的代码键:

<Style x:Key="RectStyle"> 
     <Setter Property="Width" Value="16" /> 
     <Setter Property="Height" Value="16" /> 
    </Style> 

然后:

  <StackPanel Orientation="Horizontal" > 
       <Rectangle Fill="{Binding}" Style="{StaticResource RectStyle}" /> 
      </StackPanel> 

获取其风格适用于所有您需要定义类型的元素如下:

<Style TargetType="{x:Type Rectangle}"> 

如果您想为某个元素使用多种样式并选择您想要应用的样式,请使用前者。

Source

+0

OOO,但为什么第一次的工作作风? '