NotifyProperty通过代码更改 [英] NotifyPropertyChanged through code

查看:73
本文介绍了NotifyProperty通过代码更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class that contains two other objects.

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

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

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

A similar variable in the other object.

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

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; }
    }

第1类:

    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类中工作,但是他没有参加我在包含两个变量的类中创建的事件,为什么?

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

推荐答案

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

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.

第二,在Var1的设置器中,您注册了change事件,但未设置任何内容,这是故意的吗?

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.

这篇关于NotifyProperty通过代码更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆