• WPF 依赖附加属性


    附加属性的本质是一个依赖属性,与普通的依赖属性区别:

    1:注册方法名不一样,如 DependencyProperty.RegisterAttached

    2:没有普通的属性包装器,而是通过get和set属性来实现属性包装

    3:没有普通的.NET属性

    public static readonly DependencyProperty KeepAliveProperty =  DependencyProperty.Register(
    "KeepAlive",
    typeof(bool),
    typeof(Window),
    new PropertyMetadata(KeepAliveChanged));
            /// <summary>
            /// 获取一个值,该值指示是否在现代化框架实例中保持指定对象的活动状态/// </summary>
            /// <param name="o">The target dependency object.</param>
            /// <returns>Whether to keep the object alive. Null to leave the decision to the ModernFrame.</returns>
            public static bool? GetKeepAlive(DependencyObject o)
            {
                if (o == null) {
                    throw new ArgumentNullException("o");
                }
                return (bool?)o.GetValue(KeepAliveProperty);
            }
    
            /// <summary>
            /// 设置一个值,该值指示是否在现代化框架实例中保持指定对象的活动状态/// </summary>
            /// <param name="o">The target dependency object.</param>
            /// <param name="value">Whether to keep the object alive. Null to leave the decision to the ModernFrame.</param>
            public static void SetKeepAlive(DependencyObject o, bool? value)
            {
                if (o == null) {
                    throw new ArgumentNullException("o");
                }
                o.SetValue(KeepAliveProperty, value);
            }
            private static void KeepAliveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var window = d as Window;
                if (window != null)
                {
                    window.DialogResult = e.NewValue as bool?;
                }
            }

    用法:

  • 相关阅读:
    C艹老师布置的思考题
    计蒜客练习题:网页跳转(java / C++仔细)
    计蒜客练习题:水果店(嵌套map)
    计蒜客练习题:蒜头君面试(map + max_element)
    小希的迷宫 HDU 1272(并查集)
    OpenJ_Bailian 1061
    Aizu 2224(并查集)
    Aizu 0189 (最短路)
    POJ 2377(最大生成树)
    OpenJ_Bailian 4118(dp)
  • 原文地址:https://www.cnblogs.com/wgx0428/p/11294603.html
Copyright © 2020-2023  润新知