2010-09-18 64 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,但爲什麼第一次的工作作風? '