multiple userControl instances in tabControl

风流意气都作罢 提交于 2019-11-29 08:06:35

Try this:

<TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Pages}">
    <TabControl.Resources>
        <DataTemplate x:Key="contentTemplate" x:Shared="False">
            <local:UserControl1/>
        </DataTemplate>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Header" Value="{Binding Name}"/>
            <Setter Property="ContentTemplate" Value="{StaticResource contentTemplate}"/>
        </Style>
    </TabControl.Resources>
</TabControl>

Try setting

x:Shared="False"

When set to false, modifies Windows Presentation Foundation (WPF) resource retrieval behavior such that requests for a resource will create a new instance for each request, rather than sharing the same instance for all requests.

Johannes Wanzek

You need to override the Equals() Method of your PageViewModel class.

public override bool Equals(object obj)
{
    if (!(obj is PageViewModel)) return false;

    return (obj as PageViewModel).Name == this.Name;
}

Something like this should work.

Now it is looking for the same property of the value Name. Otherwise you could also add a ID Property which is unique.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!