如何实现InotifyPropertyChanged接口
最近用了一下silverlight4.0,数据绑定方面用到InotifyPropertyChanged。publicclassInfo:INotifyPropertyCh...
最近用了一下silverlight 4.0,数据绑定方面用到InotifyPropertyChanged。
public class Info:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChange;
public void notifyPropertyChanged(string propertyName)
{
if (PropertyChange != null)
{
PropertyChange(this,new PropertyChangedEventArgs(propertyName));
}
}
private string _userName;
public string userName {
get { return _userName; }
set
{
_userName = value;
notifyPropertyChanged(userName);
}
}
}
运行后报错说是没有实现其中接口成员:“System.ComponentModel.INotifyPropertyChanged.PropertyChanged”。
按道理确实也应该实现所有接口成员才行。不知道应该怎么实现这个成员函数。而且我看到网上的代码好像都没实现这个接口成员呀。真是很郁闷,希望了解的人回答一下哈。 展开
public class Info:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChange;
public void notifyPropertyChanged(string propertyName)
{
if (PropertyChange != null)
{
PropertyChange(this,new PropertyChangedEventArgs(propertyName));
}
}
private string _userName;
public string userName {
get { return _userName; }
set
{
_userName = value;
notifyPropertyChanged(userName);
}
}
}
运行后报错说是没有实现其中接口成员:“System.ComponentModel.INotifyPropertyChanged.PropertyChanged”。
按道理确实也应该实现所有接口成员才行。不知道应该怎么实现这个成员函数。而且我看到网上的代码好像都没实现这个接口成员呀。真是很郁闷,希望了解的人回答一下哈。 展开
3个回答
展开全部
InotifyPropertyChanged接口实现分一下几个步骤:
1、定义了一个类,实现接口,实现了绑定和数据的同步(代码如下)
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged(Name);
}
}
}
}
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
2、随着定义的person对象属性越来越多,可能有年纪,毕业院校,爱好,性取向等等等,岂不是要写很多类似以上的重复代码?而且,最要命的是,以上方面采用的是硬代码,很容易编译通过,而却出错,我们要采用强类型才行!(专业性较强,2年内程序员的可能看不懂)
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged<T>(Expression<Func<T>> propertyName)
{
if (PropertyChanged != null)
{
var memberExpression = propertyName.Body as MemberExpression;
if (memberExpression != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
}
}
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged<string>(() => this.Name);
}
}
}
}
3、我们要封装这个操作,那就定义一个静态类:
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public static class IMyPropertyChangedEX
{
public static void NotifyPropertyChanged<T, TProperty>(this T iMyPropertyChanged,
Expression<Func<T, TProperty>> expression) where T : IMyPropertyChanged
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
iMyPropertyChanged.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}
4、在person里面编写绑定的属性的更改通知时,就可以这么写:
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
this.NotifyPropertyChanged(p => p.Name); ---!!!!!!
1、定义了一个类,实现接口,实现了绑定和数据的同步(代码如下)
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged(Name);
}
}
}
}
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
2、随着定义的person对象属性越来越多,可能有年纪,毕业院校,爱好,性取向等等等,岂不是要写很多类似以上的重复代码?而且,最要命的是,以上方面采用的是硬代码,很容易编译通过,而却出错,我们要采用强类型才行!(专业性较强,2年内程序员的可能看不懂)
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged<T>(Expression<Func<T>> propertyName)
{
if (PropertyChanged != null)
{
var memberExpression = propertyName.Body as MemberExpression;
if (memberExpression != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
}
}
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged<string>(() => this.Name);
}
}
}
}
3、我们要封装这个操作,那就定义一个静态类:
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public static class IMyPropertyChangedEX
{
public static void NotifyPropertyChanged<T, TProperty>(this T iMyPropertyChanged,
Expression<Func<T, TProperty>> expression) where T : IMyPropertyChanged
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
iMyPropertyChanged.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}
4、在person里面编写绑定的属性的更改通知时,就可以这么写:
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
this.NotifyPropertyChanged(p => p.Name); ---!!!!!!
意法半导体(中国)投资有限公司
2023-06-12 广告
2023-06-12 广告
单片机,即单片微控制器,也称为单片微型计算机,是将中央处理器(CPU)、存储器(ROM,RAM)、输入/输出接口和其他功能部件集成在一块 在一个小块的集成电路上,从而实现对整个电路或系统的数字式控制。单片机不是完成某一个逻辑功能的芯片,而是...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
展开全部
public event PropertyChangedEventHandler PropertyChange;
改为:
public event PropertyChangedEventHandler PropertyChanged;
然后在属性更改之后(一定是之后)触发这个事件即可:
public string UserName {
get { return _userName; }
set
{
_userName = value;
NotifyPropertyChanged("UserName");
}
}
ps:属性和方法名首字母要大写
改为:
public event PropertyChangedEventHandler PropertyChanged;
然后在属性更改之后(一定是之后)触发这个事件即可:
public string UserName {
get { return _userName; }
set
{
_userName = value;
NotifyPropertyChanged("UserName");
}
}
ps:属性和方法名首字母要大写
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
印象中好像是实现格式不正确,好久不做C#了,记不清了.
public class Info:INotifyPropertyChanged
在这个的INotifyPropertyChanged上鼠标右键,接口->实现接口,让VS自动帮你生成要实现的函数.自己再补逻辑代码
public class Info:INotifyPropertyChanged
在这个的INotifyPropertyChanged上鼠标右键,接口->实现接口,让VS自动帮你生成要实现的函数.自己再补逻辑代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询