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); }
      }
  • 相关阅读:
    科学-化学:化学百科
    科学-物理:物理学 (自然科学学科)百科
    科学-建筑学-建筑美学:建筑美学百科
    科学-建筑学:建筑学百科
    科学-哲学-美学:美学(中国哲学二级学科)
    哲学:哲学(世界观学说、社会形态之一)
    科学-语文:语文(语言和文学的简称)
    科学-分析:分析
    建模:数学建模
    科学-数学:数学
  • 原文地址:https://www.cnblogs.com/maigc249/p/5130770.html
Copyright © 2011-2022 走看看