开发者

C#. Binding object to object

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜