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); }
      }
  • 相关阅读:
    zoj2132-The Most Frequent Number
    ant-design getFieldDecorator 无法获取自定义组件的值
    ant-design-pro Login 组件 实现 rules 验证
    js 终止 forEach 循环
    js 终止 for 循环
    vue打包后出现一些map文件的解决方法
    ant font 本地化
    react 设置代理(proxy) 实现跨域请求
    ES6 async 与 await 实战
    {...formItemLayout} 标签布局
  • 原文地址:https://www.cnblogs.com/maigc249/p/5130770.html
Copyright © 2011-2022 走看看