INotifyPropertyChanged的使用
来源:互联网 发布:php教程从入门到精通 编辑:程序博客网 时间:2024/05/21 21:47
事件驱动是.net的核心,在某些情况我们可能要实现这样的功能:当我们修改程序中某些变量时,希望程序能够驱动界面的显示或者去完成某些功能,这就是典型的事件驱动。要实现这个功能有很多种方法,比如可以自己定义委托事件,然后注册触发。当然还有更方便的方法那就是使用INotifyPropertyChanged类。废话少说,看代码先。。
第一步:定义属性类
//静态统计数据
public class StatisticsData : INotifyPropertyChanged
{
public static event PropertyChangedEventHandler PropertyChanged=null;
// 实例个数
private static int _InstanceNum = 0;
public static int InstanceNum
{
get { return StatisticsData._InstanceNum; }
set
{
if (value != StatisticsData._InstanceNum)
{
StatisticsData._InstanceNum = value;
NotifyPropertyChanged("InstanceNum");//属性修改,通知PropertyChanged事件处理
}
}
}
}
第二步:注册事件,使用属性类
public partial class MainForm : Form
{
public MainForm()
{
Simulator.statistics.StatisticsData.PropertyChanged += new PropertyChangedEventHandler(stat_PropertyChanged);//注册事件
}
//统计数据更改通知,修改界面显示数据
void stat_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "InstanceNum"://根据属性名,处理业务逻辑
RunInstance_label.Text=Simulator.statistics.StatisticsData.InstanceNum.ToString();
break;
default:
break;
}
}
}
注意: NotifyPropertyChanged的参数是属性的名称
当修改InstanceNum属性的值时,通过事件通知修改RunInstance_label的text的值
- INotifyPropertyChanged的使用
- INotifyPropertyChanged的优雅使用
- INotifyPropertyChanged接口的简单使用
- ObservableCollection和List与INotifyPropertyChanged的使用
- 使用CallerMemberName简化InotifyPropertyChanged的实现
- 简单的INotifyPropertyChanged接口使用的示例类
- 使用INotifyPropertyChanged引发datagrid及时相应ObservableCollection记录的改变
- wpf INotifyPropertyChanged的用法
- 谈谈 INotifyPropertyChanged 的实现
- 谈谈 INotifyPropertyChanged 的实现
- 使用System.ComponentModel.INotifyPropertyChanged实现Property Changed时的自动通知
- INotifyPropertyChanged接口在Silverlight中的简单使用
- 实现排序,INotifyPropertyChanged,filter的BindingListView
- 两个重要的接口:INotifyPropertychanged和IValueConverter
- 让INotifyPropertyChanged的实现更优雅一些
- DataGridView DataSource INotifyPropertyChanged 避免闪烁的方法
- 简析SilverLight的List、ObservableCollection和INotifyPropertyChanged的异同
- 简析SilverLight的List、ObservableCollection和INotifyPropertyChanged的异同
- Struts2 文件下载
- 金融危机来了,你准备好了吗???
- 很久以前发现的 vc2008 的一个bug(关于模板匹配)
- 多个前缀号码的提取方法。
- [转]关于directshow的开发常见问题
- INotifyPropertyChanged的使用
- 设计一个高效合理的异常处理框架
- Flash组件开发研究(2):显示文章内容
- C#操作xml
- 异常的概念和Java异常体系结构
- C#中操作XML2
- 对CURL的一些研究
- Linux源码规模(代码行数)研究
- pku_1503 Integer Inquiry