WPF 精修篇 附加属性

 微软把DLL都开源了  今天看了一下 很多WPF实现内容都在里面

Reference Source

说附加属性 附加属性 是全局属性

用来规范全局变量  等等~ 是也可以 给控件 单独增加属性功能

举栗子

给textbox 增加附加属性 并不会增加 到Textbox 类里

功能 按Tab的时候 会全选 textbox2的内容

增加了一个类 并使用 propa 定义了附加属性

    public class TextChange
    {

 
        public static bool GetSelectOn(DependencyObject obj)
        {
            return (bool)obj.GetValue(SelectOnProperty);
        }

        public static void SetSelectOn(DependencyObject obj, bool value)
        {
            obj.SetValue(SelectOnProperty, value);
        }

        // Using a DependencyProperty as the backing store for SelectOn.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectOnProperty =
            DependencyProperty.RegisterAttached("SelectOn", typeof(bool), typeof(TextChange), new PropertyMetadata(false, onNotify));

        private static void onNotify(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            object item = d;
            if (item is TextBox)
            {
                (item as TextBox).GotFocus += (s, arges) =>
                {
                    (item as TextBox).SelectAll();
                };
            }


        }
    }

在textbox 界面 引用    xmlns:local="clr-namespace:WpfApplication21" 命名空间

  <TextBox HorizontalAlignment="Left" Height="23" Margin="111,133,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"  local:TextChange.SelectOn="True"  />

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慧哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值