简体   繁体   中英

WPF .NET4.0 re-use same instance of UserControl

I'd like to display the same instance of user control twice. Ive tried doing the following:

<UserControl.Resources>
    <Views:MyControl View x:Key="_uc1" MinHeight="300"/>
</UserControl.Resources>

And trying to use it in a TabControl:

<TabControl Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" >
    <TabItem >
        <TabItem.Header>
            <TextBlock Text="Header1" FontWeight="13"/>
        </TabItem.Header>

        <StackPanel  >
            <ContentControl Content="{StaticResource _uc1}"/>
        </StackPanel>
    </TabItem>
    <TabItem >
        <TabItem.Header>
            <TextBlock Text="Header2" FontWeight="13"/>
        </TabItem.Header>

        <StackPanel MinHeight="600" >
            <ContentControl Content="{StaticResource _uc1}"/>
        </StackPanel>
    </TabItem>
</TabControl>

Im getting the error message: "{"Specified element is already the logical child of another element. Disconnect it first."}"

Is what Im trying to achieve possible?

Thanks,

In WPF (and Silverlight) a control cannot be in more than one place in the visual tree. What you can do is have two separate instances of the user control, but bind their relevant properties to the same underlying source.

For example, let's say you had a Contact object and you wanted two MyControl instances to refer to the same FullName property.

<UserControl>
    <UserControl.Resources>
        <my:Contact x:Key="data" FullName="Josh Einstein" />
    </UserControl.Resources>
    <TabControl DataContext="{StaticResource data}">
      <TabItem>
        <TabItem.Header>
          <TextBlock Text="Header1" FontWeight="13" />
        </TabItem.Header>
        <StackPanel>
          <!-- instance #1 -->
          <Views:MyControl FullName="{Binding FullName, Mode=TwoWay}" />
        </StackPanel>
      </TabItem>
      <TabItem>
        <TabItem.Header>
          <TextBlock Text="Header2" FontWeight="13" />
        </TabItem.Header>
        <StackPanel>
          <!-- instance #2 -->
          <Views:MyControl FullName="{Binding FullName, Mode=TwoWay}" />
        </StackPanel>
      </TabItem>
    </TabControl>
</UserControl>

If you just want a single control to appear in multiple places in the visual tree, but not actually be interactive, you can use a VisualBrush to "paint" onto another control.

It's not. As the error indicates, a given object may only be present in a given logical tree once . This helps to ensure that the logical tree remains a tree .

If you're using the MVVM pattern (or are just using DataBinding in general,) then you can bind two different UserControls to the same backing ViewModel/data, so that the controls will behave the same and operate on the same state representation. You'll still need two different controls, though.

You can not have the same control in two places, but you make it jump, see this answer of mine for an example of how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM