WPF从类库中加载样式,测试使用依赖属性和附加属性

新建类库 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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值