.net c# NotifyPropertyChanged 通过代码

NotifyPropertyChanged through code
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我有一个包含其他两个对象的类.

第一个对象中的变量绑定到WPF元素,称为X.

另一个对象中的类似变量.

我希望当 PropertyChanged 事件发生时,它会更改第二个对象中的变量.

这是对我不起作用的代码:

包含变量的类:(我已注册到属性更改事件)

private Class1 _var1;
    public Class1 Var1
    {
        get { return _var1; }
        set
        {
            _var1= value;
            if (_var1!= null)
                _var1.PropertyChanged += new PropertyChangedEventHandler(_var1_PropertyChanged);
            else
                _var1.PropertyChanged -= new PropertyChangedEventHandler(_var1_PropertyChanged);
        }
    }

    void _var1_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      if(e.PropertyName=="X")
        Var2.X= Var1.X;
    }

    private Class2 _var2;
    public Class2 Var2
    {
        get { return _var2; }
        set { _var2= value; }
    }

第一类:

    private int _x;
    public int X
    {
        get { return _x; }
        set
        {
            if (_x!= value)
            {
                _x= value;
                NotifyPropertyChanged("X");
            }
        }
    }

第 2 类:

public int X { get; set; }

PropertyChanged 在班级 1 工作,但他没有参加我在包含两个变量的班级中创建的事件,为什么?

解决过程1

我不确定我是否完全理解您的意思,但如果我有一个包含 2 个变量的类,我想一起更改,我会尝试以下操作:
首先,定义一些SetAndNotify方法,否则您会因PropertyChanged事件而头疼:

public void SetAndNotify<T>(ref T field, T value, Expression<Func<T>> exp)
{
    if (!Equals(field, value))
    {
        field = value;
        OnPropertyChanged(exp);
    }
}

将它添加到将处理此事件的某个基类.

其次,在你的 setter 中为Var1你注册更改事件而不设置任何东西,这是故意的吗?

第三个也是最后一个,在 setter 中更改多个属性没有问题,但请确保您更改的是公共属性:

private SomeType privateVar1;
public SomeType PublicVar1
{
    get { return privateVar1; }
    set
    {
        SetAndNotify(ref privateVar1, value, () => PublicVar1);
        MyOtherPublicVar = someNewValue; // this will activate the property's setter.       
    }
}

我希望这有帮助.如果没有,请尝试澄清您的问题.

I have a class that contains two other objects.

A variable in the first object bind to WPF element, call it X.

A similar variable in the other object.

I want that when the PropertyChanged event happens, it will change the variable in the second object.

Here is the code that does not work for me:

The class that contains the variables: (I had register to property changed event)

private Class1 _var1;
    public Class1 Var1
    {
        get { return _var1; }
        set
        {
            _var1= value;
            if (_var1!= null)
                _var1.PropertyChanged += new PropertyChangedEventHandler(_var1_PropertyChanged);
            else
                _var1.PropertyChanged -= new PropertyChangedEventHandler(_var1_PropertyChanged);
        }
    }

    void _var1_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      if(e.PropertyName=="X")
        Var2.X= Var1.X;
    }

    private Class2 _var2;
    public Class2 Var2
    {
        get { return _var2; }
        set { _var2= value; }
    }

Class 1:

    private int _x;
    public int X
    {
        get { return _x; }
        set
        {
            if (_x!= value)
            {
                _x= value;
                NotifyPropertyChanged("X");
            }
        }
    }

class 2:

public int X { get; set; }

PropertyChanged work in class 1 but he did not come to an event I created in a class that contains the two variables, why?

Solutions1

I'm not sure I understand exactly what you mean, but if I had a class with 2 variables that I wanted to change together, I would try the following:
First, define some SetAndNotify method or you'll get a headache from the PropertyChanged events:

public void SetAndNotify<T>(ref T field, T value, Expression<Func<T>> exp)
{
    if (!Equals(field, value))
    {
        field = value;
        OnPropertyChanged(exp);
    }
}

Add it to some base class that will handle this event.

Second, in your setter for Var1 you register for the change event and not set anything, is that on purpose?

Third and last, there's no problem with changing more than one property in a setter, but make sure it's the public property that you change:

private SomeType privateVar1;
public SomeType PublicVar1
{
    get { return privateVar1; }
    set
    {
        SetAndNotify(ref privateVar1, value, () => PublicVar1);
        MyOtherPublicVar = someNewValue; // this will activate the property's setter.       
    }
}

I hope this helps. If not, please try to clarify your question.

转载于:https://stackoverflow.com/questions/15240387/notifypropertychanged-through-code

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入