WPF .NET4.0重用UserControl的相同实例 [英] WPF .NET4.0 re-use same instance of UserControl

查看:97
本文介绍了WPF .NET4.0重用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>

并尝试在TabControl中使用它:

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?

谢谢

推荐答案

不是.如错误所示,给定对象只能出现在给定逻辑树一次中.这有助于确保逻辑树仍然是 tree .

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.

如果您使用的是MVVM模式(或通常仅使用DataBinding),则可以 将两个不同的UserControl绑定到同一后备ViewModel/data,以便控件表现出相同,并以相同的状态表示进行操作.不过,您仍然需要两个不同的控件.

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.

这篇关于WPF .NET4.0重用UserControl的相同实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆