如何在TabItem中设置三个UserControl?

时间:2015-08-05 08:15:38

标签: c# wpf user-controls

我已经对这个主题进行了一些谷歌搜索,我认为我必须做一些基本的事情,不需要答案,或者我试图以复杂的方式解决我的问题,需要重新考虑整个事情

我有一个创建Window的工厂对象 - 这是由一个单独的ViewModel对象中的命令调用的。工厂对象负责将datacontext分配给传递给它的数据列表。该列表可以是可变计数,列表中的每个项目都应显示在Window的TabControl上的单独选项卡上。

<TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
        <Setter Property="Header" Value="{Binding Path=VMNameAndDate, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=true}"></Setter>
    </Style>
</TabControl.ItemContainerStyle>

我目前正在使用ItemContainerStyle来设置TabItem标头。 这适用于标题,但我需要在每个TabItem上显示三个单独的UserControls 我已将UserControls的命名空间添加到Window

    xmlns:my="clr-namespace:ThisProgram.UserControls"

我试图将它们设置为资源但没有成功。你可能已经聚集了我对WPF很新。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

如果以下是您的命名空间

xmlns:my="clr-namespace:ThisProgram.UserControls"

然后

<Grid>
        <TabControl>           
            <TabItem Header="UC1" >
                <my:ucSDH ></my:ucSDH>
            </TabItem>
            <TabItem Header="UC2">
                <my:ucWDH ></my:ucWDH>
            </TabItem>

        </TabControl>
    </Grid>

您可以根据需要将DataContext附加到UserControl

如果要将itemsSource绑定到TabControl,则:

public ObservableCollection<Object> coll { get; set; }

public MainWindow()
{
    InitializeComponent();
    coll = new ObservableCollection<Object>();

    Object uc1 = new UserControl1();
    Object uc2 = new UserControl2();
    coll.Add(uc1);
    coll.Add(uc2);

    tab1.ItemsSource = coll;
}

在Xaml:

<TabControl x:Name="tab1" ItemsSource="{Binding coll}"></TabControl>

答案 1 :(得分:0)

在经过大量挖掘并与WPF中的模板进行最终协调后,答案是:

            <TabControl Name="ViewModeListTabControl" Grid.Column="1"
                ItemsSource="{Binding  UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, Mode=OneWay}" 
                SelectedItem="{Binding Path=SelectedViewModel,
        Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=true}" ContentTemplate="{DynamicResource UCAnother}">
        <TabControl.Resources>
                <DataTemplate x:Key="UCAnother"  DataType="{x:Type my:AnotherUserControl}" >
                    <my:AnotherUserControl DataContext="{Binding}"></my:AnotherUserControl>
                </DataTemplate>
        </TabControl.Resources>

我需要将TabControl的ContentTemplate设置为包含其他UserControls的主UserControl。我必须在TabControl的资源中引用DataTemplate中的主UserControl。 现在这完全正常。