This is a migrated thread and some comments may be shown as answers.

RowDetailsTemplate with custom control in it

2 Answers 68 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Licenses
Top achievements
Rank 1
Licenses asked on 18 Feb 2011, 04:54 PM
Hello,

I have a telerik gridview that has a RowDetailsTemplate set. In that template i have a contencontrol defined as shown in the example:

<DataTemplate x:Key="VacancyRowDetailsTemplate" >                          
<ContentControl Content="{Binding ViewModel.CostCenterDistributionListViewModel.View, Source={StaticResource VMC}}" Style="{StaticResource ContentControlStyle}" />
</DataTemplate>

VMC is a custom control that allows me to reference the ViewModel, even when the datacontext of the DataTemplate is set to the selected item in the grid.

CostCenterDistributionListViewModel is a ViewModel to which a reference is held in the ViewModel of the page that holds the gridview with the rowdetails. Each time another Vacancy (item in the grid) is selected, the CostcenterDistributionListViewModel is created and initialized again.

The first time that I select a row, the details are shown as expected. But when I select another row, I get the Xaml parse exception: "Element is already the child of another element".

I've tried to solve this, by explicitly setting the CostCenterDistributionListViewModel to null before creating it again. I've also tried to Dispose the viewmodel before creating it again, but that doesn't seem to work.

When I put something else in the rowdetailstemplate (eg a TextBox), then the exception is not thrown.

Any other ideas?
Thanks,
Sodi

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Feb 2011, 08:18 AM
Hello,

 In my opinion you get this since ViewModel.CostCenterDistributionListViewModel.View is single instance. When you load the first detail you are assigning this UI element to the ContentControl Content property and if you do not clear this Content (set to null) you will get such exception. 

Best wishes,
Vlad
the Telerik team
0
Licenses
Top achievements
Rank 1
answered on 21 Feb 2011, 09:19 AM
Hello,

That is indeed what I was trying to do: setting the content to null. Simply setting CostCenterDistributionViewModel to null wasn't enough. Or maybe the timing is wrong. Now I bind the SelectedItem of the grid to a property in the viewmodel. Whenever the setter of this property is reached, I explicitly set CostCenterDistributionViewModel to null before I create it again for the newly selected row.

Thanks,
Sodi
Tags
GridView
Asked by
Licenses
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Licenses
Top achievements
Rank 1
Share this question
or