WPF: Styles not applied
i have a setup like
<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" >
<开发者_JS百科Rectangle Fill="{Binding}" />
</StackPanel>
</DataTemplate>
</ribbon:RibbonGalleryCategory.ItemTemplate>
</ribbon:RibbonGalleryCategory>
</ribbon:RibbonGallery>
my width and height are not applied to the rectangles. i wondering whats wrong
You need to give your style a Key
and then reference that key in your code:
<Style x:Key="RectStyle">
<Setter Property="Width" Value="16" />
<Setter Property="Height" Value="16" />
</Style>
Then:
<StackPanel Orientation="Horizontal" >
<Rectangle Fill="{Binding}" Style="{StaticResource RectStyle}" />
</StackPanel>
To get the style to apply to all elements of a type you need to define it like this:
<Style TargetType="{x:Type Rectangle}">
Use the former if you want to have several styles for an element and choose which one you want to apply.
Source
精彩评论