Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug DataForm: Problem Binding to nested objects
7 replies. Latest Post by steyoung on November 29, 2009.
(0)
Leonid A...
Member
636 points
137 Posts
07-20-2009 7:33 AM |
Hi, I've got a problem using DataForm. When binding to nested object and setting CommandButtonsVisibility to "Commit,Cancel", the commit button is always Disabled when changing SubEntity, and becomes Enabled when changing some property of entity. Both, entity and subentity realise INotifipropertyChanged event. I can send sample which demonstrates this issue.
BaseEntity:
using
{
PropertyChanged(
}
#endregion
MainEntity:
MainPage.Xaml:
<
</
and finally change app.startup:
mp.DataContext = oEntity;
07-21-2009 8:09 AM |
And 2 extra issues:
If I change the SubEntity property and press TAB button - cancelEdit fires, but if I get focus on MainPropery using mouse or Shift + TAB - behavior is normal
And second: error handlig issue.
Change Code of SubEntity:
public
Change SubEntity property 2 times - first enter valid string (just not empty) and then clear value - you'll see Validation summary, but double click on error will not focus on control with incorrect input, it will also fire CancelEdit.
Is it right forum?
07-22-2009 2:44 AM |
Hi, is it anybody alive here? Or I need to post bug reports for codeplex?
hectorgar
203 points
87 Posts
07-22-2009 2:08 PM |
Hey Leonid, I having the same problem than you. Are you found the solution?
Im really need to bind nested objects
07-22-2009 3:42 PM |
I try to bind the data programmatically (as an intent of workaround) in the datyaform's contentLoaded event, and the result is the same, the commit button is disabled.
private void editCompanyForm_ContentLoaded(object sender, DataFormContentLoadEventArgs e) { ((TextBox)editCompanyForm.FindNameInContent("txtCompanyName")).Text = "name"; }
Regis Brid
42 points
6 Posts
07-22-2009 6:34 PM |
Unfortunately this release of the DataForm control does not support editing nested objects out of the box. This is a functionality that is on our radar for a future release. I spent some time trying to find a solution that might accommodate you.
First, I modified the entity classes as follows:
My XAML looks like this:
<Grid x:Name="LayoutRoot"> <dataFormToolkit:DataForm x:Name="df" CurrentItem="{Binding}" CommandButtonsVisibility="Commit,Cancel"> <dataFormToolkit:DataForm.EditTemplate> <DataTemplate> <StackPanel> <dataFormToolkit:DataField> <TextBox x:Name="txtMainProperty" Text="{Binding sMainProperty, Mode=TwoWay}"/> </dataFormToolkit:DataField> <dataFormToolkit:DataField> <CheckBox x:Name="chkMainProperty" IsChecked="{Binding bMainProperty, Mode=TwoWay}"/> </dataFormToolkit:DataField> <dataFormToolkit:DataField> <TextBox x:Name="txtProperty" Text="{Binding oSubEntity.sProperty, Mode=TwoWay}"/> </dataFormToolkit:DataField> <dataFormToolkit:DataField> <CheckBox x:Name="chkProperty" IsChecked="{Binding oSubEntity.bProperty, Mode=TwoWay}"/> </dataFormToolkit:DataField> </StackPanel> </DataTemplate> </dataFormToolkit:DataForm.EditTemplate> </dataFormToolkit:DataForm> </Grid>
And finally, the code behind looks like this:
public partial class MainPage : UserControl { TextBox txtProperty; CheckBox chkProperty; public MainPage() { InitializeComponent(); this.df.ContentLoaded += new System.EventHandler(df_ContentLoaded); } private void df_ContentLoaded(object sender, DataFormContentLoadEventArgs e) { if (txtProperty != null) { txtProperty.TextChanged -= new TextChangedEventHandler(txtProperty_TextChanged); } txtProperty = this.df.FindNameInContent("txtProperty") as TextBox; if (txtProperty != null) { txtProperty.TextChanged += new TextChangedEventHandler(txtProperty_TextChanged); } if (chkProperty != null) { chkProperty.Click -= new System.Windows.RoutedEventHandler(chkProperty_Click); } chkProperty = this.df.FindNameInContent("chkProperty") as CheckBox; if (chkProperty != null) { chkProperty.Click += new System.Windows.RoutedEventHandler(chkProperty_Click); } } private void chkProperty_Click(object sender, System.Windows.RoutedEventArgs e) { Entity entity = this.chkProperty.DataContext as Entity; entity.RefreshSubEntity("bProperty", chkProperty.IsChecked); } private void txtProperty_TextChanged(object sender, TextChangedEventArgs e) { Entity entity = this.txtProperty.DataContext as Entity; entity.RefreshSubEntity("sProperty", txtProperty.Text); } }
This seems to work fine, but it did require the addition of a public method Entity.RefreshSubEntity(...), and new application code. I tried to treat the inner class as if it were a simple type rather than a complex object. It is far from being ideal. No doubt there are improvements we could make in the DataForm to support this scenario out of the box, especially if the inner sub-class implemented IEditableObject as well, or a richer interface. In the meantime, this is the best I could come up with for now.
Hope it helps.
-Regis [MSFT]
07-23-2009 4:34 AM |
Regis Brid, thank for your solution, it semms to work fine.
But I don't like codebehind in my view, so i changed dataform to autoEdit, removed all buttons, and added my own Buttons, using attached property, which enables ICommand interface. So, validation works fine, properties of viewmodel change and commit/cancel I've wrote myself
steyoung
56 points
32 Posts
11-29-2009 12:09 AM |
I've hit this issue too. Is there any update on a possible fix ?
Thanks, Steve