• 简单的应用依赖属性


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

     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
  • 相关阅读:
    Tool工具页面代码
    Tool工具生成代码数据库Model生成代码
    类别切换 分页
    ASP.NET AJAX无刷新验证用户名
    VSS的配置和使用
    js 常用方法大全
    灵异——1995年北京330路公交车失踪案
    C#用HttpWebRequest通过代理服务器验证后抓取网页内容 。。。。。
    win2003远程 客户端无法连接到远程计算机。
    .net中点击button按钮显示下一条记录(上一条 下一条)
  • 原文地址:https://www.cnblogs.com/smartsensor/p/3151940.html
Copyright © 2020-2023  润新知