zoukankan      html  css  js  c++  java
  • 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); }
      }
  • 相关阅读:
    Python3-笔记-E-005-库-系统os
    Python3-笔记-E-004-库-日历calendar
    Python3-笔记-E-003-库-日期时间datatime
    Python3-笔记-E-002-库-Unix纪元时间戳time
    Python3-笔记-E-001-库-随机数random
    Python3-笔记-D-001-异常与断言
    Python3-笔记-C-007-函数-导入模块并调用
    【bzoj2199/Usaco2011 Jan】奶牛议会——2-sat
    【bzoj1578/Usaco2009 Feb】Stock Market 股票市场——完全背包
    【bzoj1741/Usaco2005 nov】Asteroids 穿越小行星群——最小割
  • 原文地址:https://www.cnblogs.com/maigc249/p/5130770.html
Copyright © 2011-2022 走看看