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

Specified element is already the logical child of another element error with my usercontrol in CellEditTemplate.

8 Answers 272 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 24 Oct 2013, 11:12 AM
Hi
I have defined a user control that I display in a CellEditTemplate. I get the error "Specified element is already the logical child of another element... " in the second attempt to edit the cell. It displays my usercontrol successfully on the first edit attempt. My usercontrol is exposed via a viewmodel exposed as a property of my datacontext's viewmodel.

Here's an extract of the XAML.
<telerik:GridViewDataColumn.CellEditTemplate>
    <DataTemplate>
        <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <telerik:RadDropDownButton DockPanel.Dock="Top" HorizontalAlignment="Stretch" Height="22"
                                       Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AcctVm.SelectedAccount.Account_Code,Mode=TwoWay}"
                                       VerticalAlignment="Center" HorizontalContentAlignment="Left">
                <telerik:RadDropDownButton.DropDownContent>
                        <ContentControl Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AcctVm.View}"
                                        VerticalAlignment="Stretch" Height="300" />
                </telerik:RadDropDownButton.DropDownContent>
            </telerik:RadDropDownButton>
        </DockPanel>
    </DataTemplate>
</telerik:GridViewDataColumn.CellEditTemplate>

Is there a problem with this XAML or do you need more information?
Thanks
Craig

8 Answers, 1 is accepted

Sort by
0
Craig
Top achievements
Rank 1
answered on 28 Oct 2013, 02:57 PM
I think this is caused by the data bindings. I may also not have implemented all the required 'plumbing' in my user control to facilitate what I want to do, which is this:
  1. Load a grid with items.
  2. Implement a CellEditTemplate that displays my user control (currently I am hosting it in a RadDropDown button) when one of the grid cells is edited.
  3. When an item in my usercontrol is selected, I want the selected value to be used to update the cell value in the grid.
(In essence a look up to a reference table where my user control makes it easy to view the refererence data structure and controls what may be selected).

I'm trying to find a similar example but I haven't had any luck yet.

Are there any samples that may be similar?

Thanks
Craig

 
0
Yoan
Telerik team
answered on 29 Oct 2013, 09:41 AM
Hello Craig, 

I would suggest you to take a look at this and this articles, where you can find more information about this type of problem. I believe you will find them helpful.

Regards,
Yoan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Craig
Top achievements
Rank 1
answered on 29 Oct 2013, 10:19 AM
Hi Yoan,
Thanks for your reply. I've now removed the ContentControl and host my view directly in the DropDownContent. The error no longer occurs but I am unable to establish data binding in the scenario I described.
I'm wondering if attached properties would help but I've no experience in using them.
I'm still looking for a sample.
Regards,
Craig 
0
Craig
Top achievements
Rank 1
answered on 30 Oct 2013, 03:25 PM
I think I'm getting closer. I've defined a DependencyProperty on the child view but I'm still having difficulty wiring this up to the grid cell value.

In summary, I've defined a user control that is composed of number of controls that allows a user to select an item. The selected item is exposed as a property of the view model. I want the selected item to be linked to the DependencyProperty and I want the item bound to the cell in the parent view's radgridview to be bound to the selected item.

Is there a general pattern and associated sample somewhere?

0
Yoan
Telerik team
answered on 01 Nov 2013, 11:45 AM
Hello Craig,

In order to help you further, I will need some more information. May I ask you to share your implementation? It would be even better if you can send me a demo project which I can check locally. 

Looking forward to hearing from you.

Regards,
Yoan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Craig
Top achievements
Rank 1
answered on 01 Nov 2013, 02:40 PM
Hi Yoan,
I've opened ticket 754103 with the sample solution attached.
I've simplified it by not using my more complicated user control because I think it may be answered using a RadComboBox.
In the grid view the first two columns are read only. In the third column I'm using a GridViewComboBoxColumn. In this column the grid dept_code value is changed when I select a new value from the list (although I have to click out of the cell for the trigger; not sure why).
The last column demonstrates the issue I have. Because I want to use a more sophisticated selection mechanism I host the usercontrol in a CellEditTemplate (in this example a RadComboBox). The binding I have defined does not seem to link the grid Dept_Code with the selected item Account_Code which is what I need.
I would like to achieve this using binding and I suspect I need to amend my user control in some way to do it but I do not know how.

Thanks
Craig
0
Craig
Top achievements
Rank 1
answered on 03 Nov 2013, 08:38 PM
Hi Yoan,
I reviewed the test project again over the weekend since it did not really replicate what I was currently doing in my project proper.
I'm glad to say I have managed to get it to work as I would like using a dependency property. It was down to my poor understanding of dependency properties and data binding. (I can't believe how much time I've wasted trying to fix this).
I have attached an updated version of the test project to the support ticket should you want to make it available on the forum. I've added another column which displays a simple user control that exposes a dependency property.
Joel Cochran's sample project at PracticalMVVM.com and Colin Eberhardt's article 'A Simple Pattern for Creating Re-usable Usercontrols in WPF / Silverlight' (http://www.scottlogic.com/blog/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html) helped me return to basics and solve the issue. Of course, not forgetting to thank you at Telerik for your patience.
Craig
0
Yoan
Telerik team
answered on 04 Nov 2013, 09:33 PM
Hi Craig,

I am glad to hear that you have resolved the problem by yourself.

Please if you face any further difficulties do not hesitate to contact us.

Regards,
Yoan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Craig
Top achievements
Rank 1
Answers by
Craig
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or