C# WPF 前后台Two Way绑定

来源:互联网 发布:淘宝免费申请试用报告 编辑:程序博客网 时间:2024/04/30 00:37

WPF中从绑定ItemSource有TwoWay的方法,也就是前台变了,后台绑定的对象也会改变。

但是后台必须要有使用接口INotifyPropertyChanged。

代码如下:

using System.ComponentModel;namespace My.ViewModelBase{public class ViewModelBase: INotifyPropertyChanged{#region INotifyPropertyChanged Memberspublic event PropertyChangedEventHandler PropertyChanged;#endregionpublic void OnPropertyChanged(string propertyName){var handler = this.PropertyChanged;if(handler != null) handler(this, new PropertyChangedEventArgs(propertyName));}}}


ViewModel

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ExcelAddIn1.ViewModelBase;namespace ExcelAddIn1.UserControl.ViewModel{public class DayEvents: ViewModelBase.ViewModelBase{private string _startTime;public string StartTime{ get{return _startTime;}set{_startTime = value;OnPropertyChanged("StartTime");}}}}


Xaml

 <TextBox Text="{Binding myObject.StartTime, Mode=TwoWay}" />


 

原创粉丝点击