• 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); }
      }
  • 相关阅读:
    c# 把对象加入队列,对象为全局变量,对象改变队列值也跟着改变
    C# 一个数组未赋值引发的错误
    c# 2016QQ自动登录程序
    当时钟事件声明为过程变量 让system.threading.timer时钟失效
    if 循环的深入理解 哈希表的一种应用
    VB6对象与地址相互转换
    VB6的函数指针传递
    .net framework 4.0 从 GAC 卸载 程序集
    .net framework 4.0 从 GAC 卸载 程序集
    GAC in CLR 3.0
  • 原文地址:https://www.cnblogs.com/maigc249/p/5130770.html
Copyright © 2020-2023  润新知