zoukankan      html  css  js  c++  java
  • 简单的应用依赖属性

    下面代码是常用的包装方式,是死的,好好练熟,使劲敲。

     public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name",typeof(string),typeof(Student),new UIPropertyMetadata("王俊鹏"));
    
            public string Name
            {
                get { return (string)GetValue(NameProperty); }
                set { SetValue(NameProperty, value); }
            }
    View Code

    有快捷方式的,propa Get/Set 附加属性用的;propdp,常用包装。

    下面的全部的依赖属性代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Data;
    
    namespace DependencyTest
    {
        class Student : DependencyObject
        {
    
            public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student), new UIPropertyMetadata("王俊鹏"));
    
            public string Name
            {
                get { return (string)GetValue(NameProperty); }
                set { SetValue(NameProperty, value); }
            }
    
            private static void OnChangeAgeValue(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                MessageBox.Show("Change NewAge is" + e.NewValue.ToString() + " OLD AGE is" + e.OldValue.ToString());
            }
    
            private static object OnCoerceValue(DependencyObject d, object value)
            {
                MessageBox.Show("Coerce Age is" + value.ToString());
                return value;
            }
    
            private static bool IsValidateValue(object value)
            {
                Console.WriteLine("ValidateValue value is {0}", value);
                return true;
            }
    
            public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(Student),
                new FrameworkPropertyMetadata((int)100,
                FrameworkPropertyMetadataOptions.None,
                new PropertyChangedCallback(OnChangeAgeValue),
                new CoerceValueCallback(OnCoerceValue)),
                new ValidateValueCallback(IsValidateValue));
    
            public int Age
            {
                get { return (int)GetValue(AgeProperty); }
                set { SetValue(AgeProperty, value); }
            }
    
            /// <summary>
            /// 把其它控件的属性绑定到自身的依赖属性上
            /// </summary>
            /// <param name="dp">自身依赖属性</param>
            /// <param name="bind">其它控件的属性</param>
            /// <returns></returns>
            public BindingExpressionBase SetBinding(DependencyProperty dp, Binding bind)
            {
                return BindingOperations.SetBinding(this, dp, bind);
            }
        }
        /// <summary>
        /// 要附加的属性
        /// </summary>
        class School : DependencyObject
        {
            public static int GetGrade(DependencyObject obj)
            {
                return (int)obj.GetValue(GradeProperty);
            }
    
            public static void SetGrade(DependencyObject obj, object value)
            {
                obj.SetValue(GradeProperty, value);
            }
    
            // Using a DependencyProperty as the backing store for GetGrade.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty GradeProperty =
                DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School));
    
        }
    }
    View Code

    下面是前台调用的几种方式以及附加属性

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                Student stu = new Student();
                //附加属性
                School.SetGrade(stu, 6);
                this.label1.Content = School.GetGrade(stu).ToString();
                //1
                //stu.Age = Convert.ToInt32(this.textBox1.Text.Trim());
                //this.textBox2.Text = stu.Age.ToString();
    
                //2
                //Binding bindAge = new Binding("Text") { Source = this.textBox1 };
                //BindingOperations.SetBinding(stu, Student.AgeProperty, bindAge);
                //this.textBox2.Text = ((int)stu.GetValue(Student.AgeProperty)).ToString();
    
                //3 最牛逼的方式了
                Binding bindAge = new Binding("Text") {Source=this.textBox1 };
                stu.SetBinding(Student.AgeProperty, bindAge);
                this.textBox2.Text = stu.Age.ToString();
               
                //4
                //stu.SetValue(Student.AgeProperty, Convert.ToInt32(this.textBox1.Text.Trim()));
                //this.textBox2.Text = ((int)stu.GetValue(Student.AgeProperty)).ToString();
            }
    View Code
  • 相关阅读:
    python模块添加
    Python 的列表排序
    python中文处理问题
    排序算法堆排序
    搜索二分搜索
    排序算法(随机)快速排序(递归)
    排序算法计数排序
    OO设计原则总结
    异常控制以及进程调度
    ubuntu12.04 alternate win7 双系统安装
  • 原文地址:https://www.cnblogs.com/smartsensor/p/3151940.html
Copyright © 2011-2022 走看看