Apply same style to multyple objects that match specific criteria wpf
In my application I have several menus with the same background color, corner radius,border color and border thickness. I save those properties as resources in my application resources so that if I change the background color for example it will change the color of all my menus. I was wondering if it is possible to create a style so that I can just bind that style to the menus instead of binding so many properties. Moreover I just want to apply that style to some borders because not all borders in my application are menus. How could I do that?
for example I am hoping to do something like:
here is my style in my application resources:
<Style x:Key="someStyle">
<Style.Resources>
<Color x:Key="MenuBackground2">#BB252525</Color>
<CornerRadius x:Key="CornerRadiusMenu2">7</CornerRadius>
<Border x:Key="MainBorder2" >
<Border.Background>
<SolidColorBrush Color="{DynamicResource MenuBackground}"/>
</Border.Background>
</Border>
</Style.Resources>
</Style>
and then on MainWindow.xaml place:
<Border Margin="498,90,25,0" Name="brdMain" Style="{DynamicResource someStyle}"></Border>
but when I do that, brdMain does not seem to bind to t开发者_如何转开发hat style...
Your Style
is not correctly defined. It should be a collection of Setters
. For example, your given style should be:
<Style x:Key="someStyle">
<Setter Property="Background" Value="#BB252525"/>
<Setter Property="CornerRadius" Value="7"/>
etc ...
</Style>
A Style
is little more than a bunch of property setters. You can only set properties of the object to which the Style
has been applied.
<Style x:Key="someStyle" TargetType="{x:Type Border}">
<Setter Property="Control.Background" Value="#BB252525"/>
<Setter Property="Control.BorderThickness" Value=".5"/>
<Setter Property="Control.BorderBrush" Value="White"/>
<Setter Property="CornerRadius" Value="7"/>
</Style>
精彩评论