Sjeijoet
Sjeijoet

Reputation: 741

Open referenced control in a new window

Issue

I want to load a referenced control from the main window into a new window. The referenced control is already child of the main window, causing the following exception when attempting to render the new window:

System.ArgumentException was unhandled:

Must disconnect specified child from current parent Visual before attaching to new parent Visual.

I do not want to disconnect it from the main window and I also cannot create a new instance of the control since I do not know how it's instantiated or what members are applied.

Background

I'm developing an application that allows developers to extend the application with additional views of configuration options. The container of these views may turn out to be too small for large view extensions (imagine a scheduling control for agendas as example), so I wish to provide the user with the ability to open the extended view in a new window.

Code

So far I've created a behavior to attach to Hyperlinks that opens a new window with the referenced control upon the Click event. The following code is the most basic implementation to demonstrate my intention:

public class ExpandViewBehavior : Behavior<Hyperlink>
{
    public static DependencyProperty ViewProperty = DependencyProperty.Register("View", typeof(object), typeof(ExpandViewBehavior));

    public object View
    {
        get { return GetValue(ViewProperty); }
        set { SetValue(ViewProperty, value); }
    }

    protected override void OnAttached()
    {
        this.AssociatedObject.Click += AssociatedObject_Click;
    }

    void AssociatedObject_Click(object sender, RoutedEventArgs e)
    {
        if (View != null)
        {
            var window = new Window()
            {
                Content = View
            };
            window.Show();
        }
    }
}

Attached to a Hyperlink in the main window, referencing a simple TextBox to load in the new window. Where i is the System.Windows.Interactivity namespace and local my project namespace.

xmlns:local="clr-namespace:WpfApplication"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

...

<StackPanel>
    <TextBlock>
        <Hyperlink>
            <i:Interaction.Behaviors>
                <local:ExpandViewBehavior
                    View="{Binding Source={x:Reference SomeControl}}" />
            </i:Interaction.Behaviors>
            <TextBlock
                Text="(Open in new window)" />
        </Hyperlink>
    </TextBlock>
    <TextBox
        x:Name="SomeControl" />
</StackPanel>

My question is, is there is a way to load the referenced control without disconnecting it from the main window?

Upvotes: 0

Views: 116

Answers (1)

Alan
Alan

Reputation: 3002

As you can't display the same control in two places at once, you will either need to disconnect the control from the main window (as noted in the error text), or create a copy of the control to place into the child window.

You can clone the control by exporting its XAML, and then create a new control from this XAML. See this answer How can you clone a WPF object? for more details.

Upvotes: 1

Related Questions