UI分离的数据绑定 完全在cs代码中实现

 namespace BindingWPF
{
    public partial class MainWindow : Window
    {
       private Person Person;  
        public MainWindow()
        {
            InitializeComponent();
            Person = new Person("zhang", 18, new DateTime(2008, 5, 1));
            UIdataBinding();          
        }
        private void UIdataBinding()
        {          
          
            BindText(this.textBox1,Person, "Name"); // 绑定Name成功
            Person.Name = "Li";

             //this.textBox2 绑定Age
             //this.textBox2 绑定 BirthDay
             //需要双向绑定
        }

        static void BindText(TextBox textBox,object obj ,string property)
        {
            DependencyProperty textProp = TextBox.TextProperty;
            if (!BindingOperations.IsDataBound(textBox, textProp))
            {
                textBox.DataContext = obj;
                Binding b = new Binding(property);
                b.Mode = BindingMode.TwoWay;
                b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                BindingOperations.SetBinding(textBox, textProp, b);             
            }
        }      
      

    }
    public class Person :INotifyPropertyChanged

    {
        private string name;
        private UInt16 age;
        private DateTime birthDay;      
        public event PropertyChangedEventHandler PropertyChanged;
        public string Name
        {           
            get{return this.name;}
            set{
                this.name=value;
                OnPropertyChanged("Name");
               }
        }
        public UInt16 Age
        {
            get { return this.age; }
            set
            {
                this.age = value;
                OnPropertyChanged("Age");
            }
        }
        public DateTime BirthDay
        {
            get { return this.birthDay; }
            set
            {
                this.birthDay = value;
                OnPropertyChanged("BirthDay");
            }
        }       
        public Person(string Name, UInt16 Age, DateTime BirthDay)
        {
            this.Name = Name;
            this.Age = Age;
            this.BirthDay = BirthDay;
        }
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

    }

    /// <summary>DateTime类和String类的数据转换</summary>      
    [ValueConversion(typeof(DateTime), typeof(String))]
    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return date.ToShortDateString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value as string;
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return DependencyProperty.UnsetValue;
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值