Binding Textbox to a child object Silverlight

Binding Textbox to a child object Silverlight

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


danielw posted on Tuesday, February 10, 2009

Hi All,

I am having a bit of a problem getting my head around the best way of getting the following schenario to work,

Problem
I have a Root Object User which has a collection NextOfKins.
Now I have set up my form with a CSLA Dataprovider that gets the the User, I then bind two way lots of text boxes to the Dataprovider  User objectwhich works fine.
I then have a combobox that is bound to the the NextOfKins followed by textboxes that I want to bind to the selected NextOfKin item in the combo box. The problem is I do not know what/how to bind these text boxes.

I have tried to create a new Dataprovider with the type of NextOfKin and then in code setting the data property of this object but the property is readonly.

I know I can bind a datagrid to the NextOfKin object through the dataprovider but there are a lot of properties on the NextOfKin so the grid would need to be massive and would be hard to read and edit.

I also realise that I could create a new root Object NextOfKin and get that object when the combobox is changed but that means the user will need to save the nextofkin each time a change is made. I would rather have a single User object that can be saved once all the changes have been made.

Is it possible to do as I would like I seem to be going round in  circles at the moment

Cheers

Daniel

RockfordLhotka replied on Tuesday, February 10, 2009

The way this is typically done in XAML is to set the grid's datacontext to the child object.

Assuming your overall data context is the root object, you might have this:

<Grid DataContext="{Binding Source={StaticResource Mydata},Path=Data}" />

Your normal controls use binding expressions just to set the Path.

But you can always have a control set its DataContext, and if that's a container control, that new data context applies to all the controls it contains.

I think you can do this:

<StackPanel DataContext="{Binding Path=ChildProperty}" />

where 'ChildProperty' is the property of the root that refers to the child object.

danielw replied on Tuesday, February 10, 2009

Hi Rocky,

Thanks for the reply.

I can see how this works for a single Child Property. But will this work for a child collection the issue I have got is how to set the controls datacontext to a specific child of the collection. ie if i select the 5th child in the collection via the dropdown i need the controls to display the data. Does the Dataprovider know about current item.

 

Cheers

Daniel

RockfordLhotka replied on Tuesday, February 10, 2009

This is what a ListBox is used for (or other similar list controls). List controls use a DataTemplate, and the controls inside that DataTemplate get bound to the current item in the list.

 

Rocky

 

 

From: danielw [mailto:cslanet@lhotka.net]
Sent: Tuesday, February 10, 2009 9:05 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Binding Textbox to a child object Silverlight

 

Hi Rocky,

Thanks for the reply.

I can see how this works for a single Child Property. But will this work for a child collection the issue I have got is how to set the controls datacontext to a specific child of the collection. ie if i select the 5th child in the collection via the dropdown i need the controls to display the data. Does the Dataprovider know about current item.

 

Cheers

Daniel



joemcd replied on Wednesday, February 11, 2009

Daniel,

Many 3rd party grids have a alternate "card" or "one record" view.  This is the way I've always solved this problem.  Bind to the grid and let it handle the 1-record-at-a-time issue. 

Joe

 

Copyright (c) Marimer LLC