如何添加控件在标签项编程与WPF MVVM [英] How to add controls in the tab item programmatically in wpf with MVVM
问题描述
我创建了一个标签控件和动态创建的TabItems,但我不知道如何添加控件到使用MVVM的的TabItems。可以在任何一个可以帮助我
I have created a tab control and Created the tabItems dynamically, but i dont know how to add controls into the tabItems using MVVM. Could any one help me
推荐答案
有几种方法可以在 WPF 编程方式添加选项卡项目code>,我要告诉你关于我的应用程序使用该如何处理一个简单的例子。
There are a few ways to programmatically add Tab Items in WPF
and I am going to show you a simple example on how I deal with this in my application.
首先,我主持的<$ C的集合$ C>的ViewModels 为的TabItems
(或工作区
为我指的是他们)在我的 MainWindowViewModel.cs
:
First I host a collection of the ViewModels
for the TabItems
(or Workspaces
as I refer to them) in my MainWindowViewModel.cs
:
private ObservableCollection<WorkspaceViewModel> _workspaces;
public ObservableCollection<WorkspaceViewModel> Workspaces
{
get
{
if (_workspaces == null)
{
_workspaces = new ObservableCollection<WorkspaceViewModel>();
}
return _workspaces;
}
}
接下来,我在引用添加到各种控件我 MainWindow.xaml
。这一点很重要,因为我们希望确保每当集都包含一个视图模型
,它会显示相应的查看
为型号
Next I add a reference to the various controls in my MainWindow.xaml
. This is important as we want to make sure that whenever the collection contains a ViewModel
that it displays the appropriate View
for that Model.
<Window.Resources>
<DataTemplate DataType="{x:Type vm:MyUserControlViewModel}">
<vw:MyUserControlView/>
</DataTemplate>
</Window.Resources>
如果您有多个类型的用户控件的您只需在此处添加它们全部是这样的:
If you have multiple types of UserControls you simply add them all here like this:
<Window.Resources>
<DataTemplate DataType="{x:Type vm:FirstUserControlViewModel}">
<vw:FirstUserControlView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SecondUserControlViewModel}">
<vw:SecondUserControlView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ThirdUserControlViewModel}">
<vw:ThirdUserControlView/>
</DataTemplate>
</Window.Resources>
接下来,我们添加的TabControl
,并将其绑定。我们的工作
收藏
Next we add the TabControl
and bind it to our Workspace
Collection.
<TabControl ItemsSource="{Binding Workspaces}"/>
然后我只需添加我的的ViewModels
来的收藏有他们在的TabControl
显示出来。
Then I simply add my ViewModels
to the Collection to have them show up in the TabControl
.
Workspaces.Add(new FirstUserControlViewModel());
Workspaces.Add(new SecondUserControlViewModel());
Workspaces.Add(new ThirdUserControlViewModel());
我的 WorkspaceViewModel
我立足<$的C $ C> TabItem的采集非常简单,看起来是这样的:
My WorkspaceViewModel
that I base the TabItem
collection of is very simple and looks something like this:
public abstract class WorkspaceViewModel : BaseViewModel
{
public String HeaderText { get; set; }
public override string ToString()
{
return HeaderText;
}
}
添加TabItem的:
要创建一个 TabItem的
您只需创建一个用户控件
和视图模型
就像你通常会使用WPF和MVVM模式。
To create a TabItem
you simply create a UserControl
and ViewModel
like you normally would using WPF and the MVVM pattern.
namespace MyApplication.ViewModel
{
public class FirstUserControlViewModel : WorkspaceViewModel
{
public FirstUserControlViewModel ()
{
base.HeaderText = "My First Tab";
}
}
}
接下来,您需要绑定查看
至新的视图模型
。
<DataTemplate DataType="{x:Type vm:FirstUserControlViewModel }">
<vw:FirstUserControlView/>
</DataTemplate>
然后创建的实例视图模型
并将其添加到集合中的 MainWindowViewModel
。
Then you create an instance of the ViewModel
and add it to the collection in your MainWindowViewModel
.
FirstUserControlViewModel firstvm = new FirstUserControlViewModel();
Workspaces.Add(firstvm);
和现在 TabItem的
应显示。你的的TabControl
使用动态扩展的加载的TabItems:
在某些情况下,甚至可能需要从插件加载的TabItems
动态无主机应用程序首先了解有关 TabItem的
。在这种情况下,你需要有插件注册查看
和视图模型
的应用领域。
In some cases you might even need to load TabItems
from plugins dynamically without the host application first knowing about the TabItem
. In these cases you need to have the plugin register the View
and ViewModel
with the application domain.
这是很容易做到的,实际上这是我为我的 MEF
根据项目之一做。我在这里有一个职位 ,有一些额外的细节,以及
This is very easy to do, and actually something I do for one of my MEF
based projects. I have an post here, with some additional details as well.
所有你需要做的就是添加一个资源词典
来你的插件/展期,并确保主机应用程序加载一次插件已导入。
All you need to do is add a Resource Dictionary
to your plugin/extension and make sure that the host application loads it once the plugin has been imported.
要告诉你一个快速的例子,我将有一个 View.xaml
在我的扩展:
To show you a fast example I would have a View.xaml
in my extensions:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:MyExtension.Test">
<DataTemplate DataType="{x:Type vw:TestViewModel}">
<vw:TestView/>
</DataTemplate>
</ResourceDictionary>
然后我使用MEF像这样的主机暴露ResourceDictinary:
I then expose the ResourceDictinary using MEF to the Host like this:
private ResourceDictionary _viewDictionary = new ResourceDictionary();
public ResourceDictionary Dict
{
get
{
return _viewDictionary;
}
}
_viewDictionary.Source =
new Uri("/MyExtension.Test;component/View.xaml",
UriKind.RelativeOrAbsolute);
最后你用 Application.Current.Resources.MergedDictionaries.Add
的View.xaml加载到主机上。
Last you use Application.Current.Resources.MergedDictionaries.Add
to load the View.xaml into the host.
这篇关于如何添加控件在标签项编程与WPF MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!