WPF常用代码:依赖属性

  来源:http://www.snippetsource.net/Snippet/20/define-a-custom-dependencyproperty

  1. 获取对象附加属性
    public IEnumerable<DependencyProperty> GetAttachedProperties(DependencyObject element)
    {
        var markupObject = MarkupWriter.GetMarkupObjectFor(element);
        foreach (MarkupProperty mp in markupObject.Properties)
        {
            if (mp.IsAttached && mp.DependencyProperty != null)
            {
                var dpd = DependencyPropertyDescriptor.FromProperty(mp.DependencyProperty, element.GetType());
                if (dpd != null)
                {
                    yield return dpd.DependencyProperty;
                }
            }
        }
    }
  2. 监听依赖属性更改
    var descriptor = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
     
    if (descriptor != null)
    {
        descriptor.AddValueChanged(myTextBox, delegate
        {
            // Add your propery changed logic here...
        });
    } 
  3. 定义附加属性
    public static readonly DependencyProperty TopProperty =
        DependencyProperty.RegisterAttached("Top", 
        typeof(double), typeof(Canvas),
        new FrameworkPropertyMetadata(0d,
            FrameworkPropertyMetadataOptions.Inherits));
     
    public static void SetTop(UIElement element, double value)
    {
        element.SetValue(TopProperty, value);
    }
     
    public static double GetTop(UIElement element)
    {
        return (double)element.GetValue(TopProperty);
    }
  4. 定义依赖属性
    // Dependency Property Declaration
    public static readonly DependencyProperty BackgroundProperty =
        DependencyProperty.Register("Background", typeof(Brush), typeof(MyControl),
        new FrameworkPropertyMetadata(Brushes.Transparent));
    
    // Property Wrapper
    public Brush Background
    {
        get { return (Brush)GetValue(BackgroundProperty); }
        set { SetValue(BackgroundProperty, value); }
    }
  5. 只读依赖属性
    // Register the private key to set the value
    private static readonly DependencyPropertyKey IsMouseOverPropertyKey = 
          DependencyProperty.RegisterReadOnly("IsMouseOver", 
          typeof(bool), typeof(MyClass), 
          new FrameworkPropertyMetadata(false));
     
    // Register the public property to get the value
    public static readonly DependencyProperty IsMouseoverProperty = 
          IsMouseOverPropertyKey.DependencyProperty;    
     
    // .NET Property wrapper
    public int IsMouseOver
    {
       get { return (bool)GetValue(IsMouseoverProperty); }
       private set { SetValue(IsMouseOverPropertyKey, value); }
    }

 

posted @ 2016-01-14 16:58  magic249  阅读(239)  评论(0编辑  收藏  举报