RowDetailsTemplate with custom control in it

3 posts, 0 answers
  1. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 18 Feb 2011 Link to this post

    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. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Feb 2011 Link to this post

    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
  3. DevCraft banner
  4. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 21 Feb 2011 Link to this post

    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
Back to Top