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
  • 相关阅读:
    标题党的进步:道字大旗不再扯,美为号召又开张
    dwr自动生成的js文件到底在哪里?
    JavaScript全局优化带来的负面效果……
    内训资料公开:设计师的实战过程(1)
    元语言基础技术之:在JS中如何自由地创建函数
    QoBean的元语言系统(一)
    Oracle面向服务的架构
    对JavaScript的eval()中使用函数的进一步讨论~
    KEGG and Gene Ontology Mapping in Bioinformatic Method
    mysql user administration
  • 原文地址:https://www.cnblogs.com/smartsensor/p/3151940.html
Copyright © 2011-2022 走看看