开发者

C#. Binding object to object

开发者 https://www.devze.com 2022-12-25 23:07 出处:网络
I develop the part of app functionality. I have the databinding object \'A\' and another object \'B\'.

I develop the part of app functionality. I have the databinding object 'A' and another object 'B'.

public class SomeProxy
{       
    public SomeProxy(BindingSource A)
    {          
        var b = new B(A.SomeProperty);      开发者_运维知识库    
    }
}

public class B
{
    public B(List<T> somePropertyFromA)
    {
        SomePropertyFromA = somePropertyFromA;
    }

    public int SomePropertyFromA{get;set;}

    //Some autocalculated propeties, that are based on SomePropertyFromA

    //I want to reculculate them, when the SomePropertyFromA will be changed
}

'B' object culculates some values, based on the property 'a' of the 'A' object. I want to bind property in object 'B' to the same property 'a' in 'A'. It's need to handle on changes of the 'a' property, because some UI controls are binded to 'B'-s properties. How can I do this?

Sorry for some oddness. Thanks


Do you mean you need a bi-directional link?

Both A and B can have properties that point to each other. Just be sure to create object B -outside- of A, so that you can do dependency injection. Order of things should be:

B objectB = new B();
A objectA = new A(objectB);

Basic rule: do not create objects in constructors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号