inotifypropertychanged(inotifypropertychanged和依赖属性)

http://www.itjxue.com  2023-02-23 05:24  来源:未知  点击次数: 

WPF 如何动态绑定static成员变量 并实现INOTIFYPROPERTYCHANGED方法

首先创先一个实体类

public class MyClass : INotifyPropertyChanged

{

private static string _myString = "sdfdsfssdfsdfsdfdsf";

public string MyString

{

get { return _myString; }

set

{

_myString = value;

OnPropertyChanged("MyString");

}

}

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]

protected virtual void OnPropertyChanged(string propertyName)

{

PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));

}

}

//然后把这个实体类绑定到页面的 DataContext

TextBox Text="{Binding MyString}"/

追问:

我从后台绑定的,但是它说我没有实现INOtifyPropertyChanged接口

追答:

2中方式

第一种

第二种

追问:

使用第一种方法的时候它说URI指向的是未包含在程序集中的命名空间...

所以我在后台绑定的,具体代码如下:

但是程序爆出如下异常,请问该怎么办?把绑定的方法从oneway改为onetime

就可以运行,请问是什么原因:

追答:

不是这么写的吧

简单的写法

this.DataContext = new MyClass();

MytTextBox.SetBinding(TextBox.TextProperty, new Binding("MyString"){Mode = BindingMode.OneWay});

你的那种写法就不需要绑定DataContext了

直接

Binding bd = new Binding() { Source = new MyClass(), Path = new PropertyPath("MyString"),Mode = BindingMode.OneWay };

BindingOperations.SetBinding(MytTextBox, TextBox.TextProperty, bd);

追问:

你好,按照你的方式试过了,还是抛出XamlParseException错误,然后我将OneWay的调用方法改为onetime就不会出错,应该还是INotiyPropertyChanged接口没有正确实现,请问我该怎么办?

如何优雅的实现INotifyPropertyChanged接口

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 NotifyChangedT(ExpressionFuncT 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

NotifyChangedstring(() = 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 NotifyPropertyChangedT, TProperty(this T iMyPropertyChanged,

ExpressionFuncT, 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); ---!!!!!!

C# WPF中设置绑定时,INotifyPropertyChanged接口如何对数组使用

如果你这些类继承自同一个根基类的话那么只用在根基类实现该接口就行了啊

(责任编辑:IT教学网)

更多

推荐导航代码文章