c#--wpfデータバインドbinding

2716 ワード

xaml
   

    
        

MainWindow.xaml.cs


   

using System;
using System.Windows;

namespace  binding
{
    /// 
    /// MainWindow.xaml  
    /// 
    public partial class MainWindow : Window
    {
        private cd cd = new cd();   // cd binding   

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this.cd;   // cd  

       
        }



        private void btn(object sender, RoutedEventArgs e)
        {
            Random r = new Random();
              int i=  r.Next();
            cd.name = i.ToString();   // cd  name   
            
        }
    }


    public class cd: DependencyObject
    {


        public static DependencyProperty nameProperty;
        public string name
        {
            #region
            get
            {
                return (string)GetValue(nameProperty);
            }
            set
            {
                SetValue(nameProperty , value);
            }
            #endregion
        }

        public event DependencyPropertyChangedEventHandler OnDependencyPropertyChanged;
        protected static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            #region
            cd mn = (cd)d;

            if (mn.OnDependencyPropertyChanged != null)
            {
                mn.OnDependencyPropertyChanged(d, e);
            }
            #endregion
        }


        static cd()
        {

            FrameworkPropertyMetadata metadata = new  FrameworkPropertyMetadata("");   // 
            metadata.PropertyChangedCallback = new PropertyChangedCallback(PropertyChanged);
            cd.nameProperty = DependencyProperty.Register("aa", typeof(string), typeof(cd), metadata);  //  aa binding

        }


        public cd()
        {
            #region
            this.name ="aaaaaa";    //  
            #endregion
        }



    }

 
   
}