简体   繁体   English

在TabControl中使用各种ViewModel重用UserControl

[英]Reuse UserControl in TabControl with Various ViewModels

I have one user control that I would like to reuse with multiple ViewModels, all which implement the same interface. 我有一个用户控件,我想将其与多个实现相同接口的ViewModel重用。 I would like to have these embedded in a TabControl. 我想将这些嵌入在TabControl中。

Currently I can do this for a single instance but I am struggling to reuse my UserControl. 目前,我可以为单个实例执行此操作,但是我正在努力重用UserControl。 For the single instance I can either bind the ViewModel in the UserContol's xaml or instantiate it in the code behind, however I can't figure out how to set this from a higher level. 对于单个实例,我可以将ViewModel绑定到UserContol的xaml中,也可以在后面的代码中实例化它,但是我不知道如何从更高级别进行设置。

Here is what I have, 这就是我所拥有的

      <TabControl HorizontalAlignment="Left" Height="800" Margin="0,0,0,0" VerticalAlignment="Top" Width="600">
        <TabItem Header="Tab1">
            <Frame Source="SomeUserControl.xaml"  BorderThickness="0" Margin="0" />
        </TabItem>
    </TabControl>

Here is pseudo code for what I would like to achieve, 这是我想要实现的伪代码,

      <TabControl>
        <TabItem Header="Tab1">
            <Frame Source="{SomeUserControl.xaml, DataContext=ViewModel1}" />
        </TabItem>
        <TabItem Header="Tab2">
            <Frame Source="{SomeUserControl.xaml, DataContext=ViewModel2}" />
        </TabItem>
    </TabControl>

Thanks! 谢谢!

Instead of hard coding the tabs, bind to an ObservableCollection of an "item" class. 无需对选项卡进行硬编码,而是绑定到“ item”类的ObservableCollection。 Ie something like 即像

class MyTabItems : INotifyPropertyChanged
{
  public string Header...
  public object DataContext...
}

create an: 创建一个:

ObservableCollection<MyTabItems>

Bind the TabControl ItemsSource to the ObservableCollection. 将TabControl ItemsSource绑定到ObservableCollection。 In the item template for the TabControl bind the Header to the Header property and the DataContext to the DataContext property. 在TabControl的项目模板中,将Header绑定到Header属性,将DataContext绑定到DataContext属性。 The source can be hardcoded in the template or you can add another property to your item class and bind it to that if you want more flexibility. 可以在模板中对源进行硬编码,也可以在项目类中添加另一个属性,如果需要更大的灵活性,可以将其绑定到该类。

Thanks to SledgeHammer's answer I was able to take a different approach and get things working. 多亏了SledgeHammer的回答,我才能够采用另一种方法并使工作正常。

        <TabControl Name="TabControl" ItemsSource="{Binding TabItems}" HorizontalAlignment="Left" Height="800" Margin="0,0,0,0" VerticalAlignment="Top" Width="600">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <UserControl Content="{Binding DataContext}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>

TabItems was populated in the code behind associated with the xaml above. TabItems已填充在与上述xaml相关的代码中。

    this.TabItems = new ObservableCollection<TabItem>
    {
        new TabItem("Main", new MainControl()), 
        new TabItem("Tab1", new GenericTabControl(new ViewModel1())),
        new TabItem("Tab2", new GenericTabControl(new ViewModel2()))
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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