3

This question is related to following thread. Prism RegionAdapter - Removing then Adding View

My post to this thread was deleted my admin, saying it was answered and I need to start new thread. Please let me know if any of you have solved this issue.

My deleted post was... How did you manage to get rid of "Specified element is already the logical child of another element. Disconnect it first." error with closing event. I tried the same thing, on closing I remove the dockablecontent from the documentpane. But it doesn't help I keep getting this error. Once I remove the view, and then later try to add it again the same way you are adding, I get this error.

Please help me, I am not sure what am I missing. I have wasted so much of time trying to get around it but no luck yet.

Here is the code I am using:

<ad:DockableContent ...
                    x:Name="viewRoot"
                    IsCloseable="True" HideOnClose="False"
                    Title="{Binding Title}">    
        <ContentControl Grid.Row="1" Content="{Binding View}"/>
    </Grid>
</ad:DockableContent>

It gets used inside this:

<ad:DockingManager x:Name="DockingManagerControl" Grid.Column="1">
                <ad:ResizingPanel x:Name="MainResizingPanel" ResizeWidth="*" ResizeHeight="*" Orientation="Horizontal">
                    <ad:DockablePane ad:ResizingPanel.ResizeWidth="0.25*" prism:RegionManager.RegionName="ProjectBrowserRegion"/>
                    <ad:ResizingPanel x:Name="PespectiveResizingPanel" ResizeWidth="0.75*" ResizeHeight="*" Orientation="Horizontal">
                        <ad:DocumentPane x:Name="DockablePaneControl"/>
                    </ad:ResizingPanel>
                </ad:ResizingPanel>
            </ad:DockingManager>

View content bindings are UI element which comes from separate dlls, So I don't have them as View/ViewModel pair. So I can't use ContentTemplate+DataContext way here. Things load nicely, but use can select/deselect these views. So if a view has been opens the view selection dialog again, then I clear all views (I remove it from DockablePaneControl Items.Clear()), and add the selected views again (DockablePaneControl Items.Add()), then I get this error.

Rajiv
  • 1,426
  • 14
  • 21
  • @jlafay, since you removed "Thank You" at the end of my post. Stackoverflow has locked my account to post answers and etc. Did you vote down or flagged my question? Can you help me, I wasn't active here for a while but now I am not able to post any answers here. – Rajiv Sep 20 '13 at 15:14
  • I'm sorry. I did not down vote, flag, or do anything else to your post. I just simply provided an edit. – Jeff LaFay Sep 20 '13 at 18:05

1 Answers1

6

Usually this occurs because you are trying to assign the same control to two different parents.

I often see this issue with Styles, where a non-template property that contains Controls is set in a Style, such as ContentControl.Content, or DataGridColumn.Header

For example, if your style sets ContentControl.Content, and you add two ContentControls to your Window, you'll encounter this error because the style is trying to assign the same controls inside the Content section of both ContentControls. The fix is to set a template style, such as ContentControl.ContentTemplate instead of ContentControl.Content. I like to say this is like giving multiple people the same cookie to it - it just doesn't work. You have to give them each the cookie cutter (template) instead so they can make their own.

I actually see questions about this error so much that I wrote a blog post about it

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Hi Rachel, thanks for your quick response. Yes, I am using ContentControl.Content property to set the content. I couldn't implement my stuff with ContentTemplate+DataContext because the content comes from a separate dll as whole FrameworkElement. I don't have the luxury of using MVVM here, please suggest me how can I over come this problem here. Is there a way I can get it done using ContentTemplate+DataContext? Remember I have the entire UI element to be hosted inside ContentControl. – Rajiv Nov 08 '11 at 15:48
  • @Rajiv I would need to see your source code. Click the "Edit" button on your question and add in the code which is causing the error. I also upvoted an answer on the link you posted and added a comment as to why it was correct. If your code is similar to the linked post's code, then you can try fixing it using that solution. The link for the answer is http://stackoverflow.com/questions/4993705/prism-regionadapter-removing-then-adding-view/4993910#4993910 – Rachel Nov 08 '11 at 16:01
  • @Rajiv I need to see the C# code you use to add/remove items from your UI Controls. I am fairly sure you're getting the error because you are adding the same UIElement to two different controls, which is not possible – Rachel Nov 08 '11 at 17:08
  • Yes, the underlying UI item "View" is never re-created. There are a set of view registry. So I try to wrap the same under Avalon DockableContent and try to add it again. Just like the original post (http://stackoverflow.com/questions/4993705/prism-regionadapter-removing-then-adding-view/8051884) does it. Basically it is just this line... Items.Add(new DockableContent() {Content = view)}; this view is being re-used everytime. I hope it explains. Do you get the idea of what I am trying to achieve now? – Rajiv Nov 09 '11 at 09:00
  • @Rajiv My guess is `view` has already been added to another `DockableContent`, so the error occurs because you are trying to assign it to a second `DockableContent.Content`. I can't be positive unless I see the code, but I bet if you remove the `Content = view` you wouldn't get the error. To fix it, either make `view` something other than a UI Element and use `DataTemplates` to tell WPF how to draw the class, or make sure that `view` is removed from it's old `DockableContent` before trying to add it to a new one – Rachel Nov 09 '11 at 12:52
  • ts been a while, but I should update. It was indeed a logical problem in my region adaptor. Rachel's suggestion was correct above. – Rajiv Sep 20 '13 at 15:14