Accessing child object property from parent object

Accessing child object property from parent object

Old forum URL: forums.lhotka.net/forums/t/766.aspx


fpmalard posted on Monday, July 31, 2006

Hello,

I have two objects implemented with CollectionBase framework, say MyParent and MyChild.
MyChild object is embedded into MyParent and I need to bind some MyChild properties via MyParent.

I would like to bind a MyChild property without need to create a property copy into MyParent just to allow access to the MyChild's property because this will duplicate my entire code.

So I have decided to bind the property through the MyParent object using a reference to the MyChild object. To do that I have created a property called "MyChildRef" inside MyParent which returns a reference to MyChild embedded object.

According to VStudio manual I can do the following:

"You can also use a period-delimited navigation path when you bind to an object whose properties return references to other objects (such as a class with properties that return other class objects). "

So I expect that the following binding should work (PropInChild is a MyChild property):

txtMyTextBox.SetDatabind(MyDataSource,"MyChildRef.PropInChild");

But this returns an error: "Cannot bind to property or column PropInChild on DataSource.

So my questions are:

- Is is possible to do that?
- What is the best way to put an object embedded into another and access its properties from the parent?

Regards,
Fernando Malard.

esteban404 replied on Monday, July 31, 2006

fpmalard:

txtMyTextBox.SetDatabind(MyDataSource,"MyChildRef.PropInChild");

But this returns an error: "Cannot bind to property or column PropInChild on DataSource.

Fernando,

I'm not using 2.0, but try it this way:

txtMyTextBox.SetDatabind(MyDataSource.MyChildRef, "PropInChild");

_E

fpmalard replied on Monday, July 31, 2006

esteban404:
fpmalard:

txtMyTextBox.SetDatabind(MyDataSource,"MyChildRef.PropInChild");

But this returns an error: "Cannot bind to property or column PropInChild on DataSource.

Fernando,

I'm not using 2.0, but try it this way:

txtMyTextBox.SetDatabind(MyDataSource.MyChildRef, "PropInChild");

_E

Hi esteban,

Actually we are using Custom Collections (not ADO) and your suggestion is not possible once the first binding parameter needs to be a collection and can't be an object.

We are using embedded collections inside MyParent with success once we can bind its properties this way:

txtMyTextBox.SetDatabind (MyParent.PropMyChildCollection, "PropInChild");

We could create an embedded collection with a single object to simulate an embedded object. This solves the binding problem but causes another problems when updating this object (grid refresh, etc).

Thank you anyway! Any other idea?

Brian Criswell replied on Monday, July 31, 2006

If I understand your situation correctly, you are trying to bind a property on a collection to a TextBox.  As I understand it, you can only bind a list to a list control (which a TextBox is not).  To do what you want to do, I would suggest creating a wrapper class that would not be a list and would pass through the properties you want databound on your collection.

public class MyParentWrapper
{
    private MyParent _dataSource;
    public MyParent DataSource
    {
       get { return _dataSource; }
       set { _dataSource = value; }
    }

    public string PropInChild
    {
       get { return _dataSource.MyChildRef.PropInChild; }
       set { _dataSource.MyChildRef.PropInChild = value; }
    }
}


You can then bind MyParentWrapper.PropInChild to your TextBox.

esteban404 replied on Monday, July 31, 2006

Sorry, F, nothing more hits me other than the depth of the bind. It seems the binding is missing the specific child instance where it can find the "PropInChild" value. But, like I said, not on the next plain of existence yet.

Good luck

_E

fpmalard replied on Monday, July 31, 2006

Yes esteban, I suspect exactly this.
I don't know if this is a design limitation of CSLA or if my object classes are missing something related to binding.

Brian, regarding to your solution, its not necessary to create a wrapper because I can create these properties inside MyParent mapping every MyChild property I would like to bind. This way works but it is very tedious to keep creating duplicated properties at both MyParent and MyChild.

The best solution is to bind passing through the MyParent going direct to MyChild property...but...

:(

 

 

david.wendelken replied on Tuesday, August 01, 2006

I'm not familiar with the approach you are taking to solve this problem.  (I'm still new at C# and OO programming.)

But I just learned how to bind a child collection to a grid from a parent object yesterday, and it might help you out...  It has the virtue of being really simple.

Take a look at ResourceEdit.aspx in the sample Project Tracker application.  It has a resource object that contains a collection of another object.  The resource "record-level" properties are displayed in a DetailView and the resource "assignment" properties are displayed in a GridView.  The trick to getting it to work nicely was in how the csla datasources were defined. 

Hope that helps!

Copyright (c) Marimer LLC