新建类库 BaseStyle
新建附加属性
/// <summary>
/// 新建附加属性无需继承其他类
/// 在所有控件均可使用
/// </summary>
public class Attach
{
public static Thickness GetButtonThickness(DependencyObject obj)
{
return (Thickness)obj.GetValue(ButtonThicknessProperty);
}
public static void SetButtonThickness(DependencyObject obj, Thickness value)
{
obj.SetValue(ButtonThicknessProperty, value);
}
//新增扩展属性 用于设置是否有边框
public static readonly DependencyProperty ButtonThicknessProperty =
DependencyProperty.RegisterAttached("ButtonThickness", typeof(Thickness), typeof(Attach), new PropertyMetadata(default(Thickness)));
public static Brush GetButtonBrush(DependencyObject obj)
{
return (Brush)obj.GetValue(ButtonBrushProperty);
}
public static void SetButtonBrush(DependencyObject obj, Brush value)
{
obj.SetValue(ButtonBrushProperty, value);
}
// ButtonBrush 附加属性 用于设置边框颜色
public static readonly DependencyProperty ButtonBrushProperty =
DependencyProperty.RegisterAttached("ButtonBrush", typeof(Brush), typeof(Attach), new PropertyMetadata(default(Brush)));
public static CornerRadius GetButtonRadius(DependencyObject obj)
{
return (CornerRadius)obj.GetValue(ButtonRadiusProperty);
}
public static void SetButtonRadius(DependencyObject obj, CornerRadius value)
{
obj.SetValue(ButtonRadiusProperty, value);
}
//ButtonRadius 附加属性用于设置边框角度
public static readonly DependencyProperty ButtonRadiusProperty =
DependencyProperty.RegisterAttached("ButtonRadius", typeof(CornerRadius), typeof(Attach), new PropertyMetadata(default(CornerRadius)));
}
新建 TestButton
/// <summary>
/// 依赖属性测试button
/// 依赖属性需要继承一个控件,只能在这个控件范围内使用
/// </summary>
public class TestButton:Button
{
public Brush BkColor
{
get { return (Brush)GetValue(BkColorProperty); }
set { SetValue(BkColorProperty, value); }
}
//新增依赖属性用于设置button背景色
public static readonly DependencyProperty BkColorProperty =
DependencyProperty.Register("BkColor", typeof(Brush), typeof(TestButton), new PropertyMetadata(default(Brush)));
}
新建 ButtonKeys 用于在其他类中访问资源
public class ButtonKeys
{
public static ComponentResourceKey ButtonTestStyle
{
get
{
return new ComponentResourceKey(typeof(ButtonKeys), "button.test");
}
}
}
新建资源
Themes/Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaseStyle">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BaseStyle;component/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
ButtonStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:BaseStyle"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ButtonKeys} , ResourceId=button.test}" TargetType="local:TestButton">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{ x:Type local:TestButton }">
<Border BorderThickness="{Binding Path=(local:Attach.ButtonThickness),RelativeSource={RelativeSource TemplatedParent}}"
BorderBrush="{Binding Path=(local:Attach.ButtonBrush),RelativeSource={RelativeSource TemplatedParent}}"
CornerRadius="{Binding Path=(local:Attach.ButtonRadius),RelativeSource={RelativeSource TemplatedParent}}"
Background="{TemplateBinding BkColor}"
>
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" >
<TextBlock Text="{ TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
测试使用
新建wpf项目
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:res="clr-namespace:BaseStyle;assembly=BaseStyle"
Title="MainWindow" Height="350" Width="525">
<Grid>
<res:TestButton Style="{DynamicResource {x:Static res:ButtonKeys.ButtonTestStyle}}" Width="200" Height="80" BkColor="LightYellow"
res:Attach.ButtonThickness="2" res:Attach.ButtonBrush="Red" res:Attach.ButtonRadius="5" >测试</res:TestButton>
</Grid>
</Window>
效果
修改
ComponentResourceKey key = new ComponentResourceKey(typeof(FontSizeKeys), "S.FontSize.Header");
this.Resources[key] = (double)20;