[英]WPF TabControl with ContentControl
I have searched a lot on SO and didn't find answer to my problems. 我在SO上搜索了很多,并没有找到我的问题的答案。 I want to use TabControl with MVVM.
我想将TabControl与MVVM一起使用。 Here is how I add TabControl to MainWindow.xaml
以下是我将TabControl添加到MainWindow.xaml的方法
<Window.Resources>
<DataTemplate DataType="{x:Type models:PartnersViewModel}">
<views:PartnersView />
</DataTemplate>
<DataTemplate DataType="{x:Type models:ProjectsViewModel}">
<views:ProjectsView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<models:ApplicationViewModel/>
</Window.DataContext>
<TabControl ItemsSource="{Binding PageViewModels}" TabStripPlacement="Left">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
PageViewModels is ObservableCollection<IPageViewModel>
. PageViewModels是
ObservableCollection<IPageViewModel>
。 IPageViewModel
is simple interface with one property Name
. IPageViewModel
是一个具有一个属性Name
简单接口。 There are 2 implementation of this interface PartnersViewModel
and ProjectsViewModel
. 此接口有两个实现,即
PartnersViewModel
和ProjectsViewModel
。
public class ProjectsViewModel : IPageViewModel
{
public String Name
{
get { return "Projects"; }
}
}
public class PartnersViewModel : IPageViewModel
{
public String Name
{
get { return "Partners"; }
}
}
I want each tab to be displayed as ContentControl. 我希望每个选项卡都显示为ContentControl。 Header text is taken from Name property.
标题文本取自Name属性。 My ProjectsView and PartnersView looks like this:
我的ProjectsView和PartnersView看起来像这样:
<UserControl x:Class="WANIRPartners.Views.ProjectsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
<Grid>
<Label Content="Projects Content" />
</Grid>
</UserControl>
Using this code, header and content in TabControl
is exactly the same. 使用此代码,
TabControl
标题和内容完全相同。 'Projects Content'/'Partners Content' are shown in tab headers and also(this is ok) in tab content. “项目内容”/“合作伙伴内容”显示在选项卡标题中,并且(在标签内容中也可以)。 When I change
<Label/>
to <DataGrid/>
tab headers contains datagrid (sic!). 当我将
<Label/>
更改为<DataGrid/>
标签时,标题包含datagrid(sic!)。 How can I get this working properly. 我怎样才能使这个工作正常。 I mean how can I display headers as value of property
Name
and tab content as properly rendered <views:PartnersView />
or <views:ProjectsView />
depending on what is in PageViewModels
. 我的意思是如何将标题显示为属性
Name
和标签内容的值作为正确呈现的<views:PartnersView />
或<views:ProjectsView />
具体取决于PageViewModels
。
your code should work, dont have a IDE atm. 你的代码应该工作,没有IDE atm。 you can also use Snoop to check your bindings at runtime.
您还可以使用Snoop在运行时检查绑定。 i changed the ContentTemplate to:
我将ContentTemplate更改为:
<TabControl.ContentTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding .}" />
</DataTemplate>
</TabControl.ContentTemplate>
and it works. 它的工作原理。 but even your code works in my testapp.
但即使你的代码也适用于我的testapp。
I have discovered solution for Elysium. 我发现了极乐世界的解决方案。 Adding fter adding code belowe everything work fine.
在下面添加fter添加代码一切正常。
<TabControl.Resources>
<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</TabControl.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.