如何使用tabControl在XAML中使用两个ViewModels代码

时间:2016-11-21 09:53:13

标签: c# wpf mvvm tabcontrol

我是WPF的新手请帮帮我任何一个。我有两个viewmodels 1.ItemCategoryViewModel和2.TypeViewmodel这两个视图模型来两个不同的表

<tabcontrol>
    <TabItem x:Name="itemtype" Header="ItemType" IsSelected="True" MinHeight="10"> 
        <Grid/>
    </tabitem> 
    <TabItem Header="ItemCategory"  Margin="-1,0" MinHeight="10">
        <grid/>
    </tabitem>
</tabcontrol>

在xaml.cs代码中,我想使用两个viewmodels。

TypeViewmodel povm = new TypeViewmodel();

ItemCategoryViewModel tcvm=new ItemCategoryViewModel();

public PurchaseOrderEntry()
{
    InitializeComponent();
    this.DataContext = povm;
    this.DataContext = tcvm;
    txtPONumber.Focus();

    if (povm.FocusMoveTo == null)
        povm.FocusMoveTo = new Action(() => this.FieldNumberToChange());

    if (povm.FocusMoveByTabId == null)
        povm.FocusMoveByTabId = new Action(() =>this.GoToFocusByTabId());

    if (povm.OpenDialogue == null)
        povm.OpenDialogue = new Action<string>(this.OpenDialogue);

    if (tcvm.FocusMoveTo == null)
        tcvm.FocusMoveTo = new Action(() => this.FieldNumberToChange());

    if (tcvm.FocusMoveByTabId == null)
        tcvm.FocusMoveByTabId = new Action(() =>this.GoToFocusByTabId());

    if (tcvm.OpenDialogue == null)
        tcvm.OpenDialogue = new Action<string>(this.OpenDialogue);
}

当我只执行一个标签项时,另一个标签项不起作用.viewmodel重写。我该如何解决请帮助我

2 个答案:

答案 0 :(得分:0)

如何将两个视图模型包装在“MainViewModel”中并在xaml代码中调整绑定?

据我所知,您可以在一个视图中使用不同的视图模型用于不同的控件,但我不会根据所选的选项卡更改tabcontrol的完整视图模型:

编辑:

 public partial class MainWindow : Window
 {
    public MainViewModel MainViewModel { get; set; }
    public MainWindow()
    {
        MainViewModel = new MainViewModel(new TypeViewmodel(), new ItemCategoryViewModel());
        this.DataContext = MainViewModel;
        InitializeComponent();

    }
}
public class MainViewModel
{
    public TypeViewmodel TypeViewmodel { get; set; }
    public ItemCategoryViewModel ItemCategoryViewmodel { get; set; }

    public MainViewModel(TypeViewmodel povm, ItemCategoryViewModel tcvm)
    {
        this.TypeViewmodel = povm;
        this.ItemCategoryViewmodel = tcvm;
    }
}

public class ItemCategoryViewModel
{
    public ItemCategoryViewModel()
    {
        TextBoxMessageItemCategoryViewModel = "Test - ItemCategoryViewModel";
    }
    public String TextBoxMessageItemCategoryViewModel { get; set; }
}

public class TypeViewmodel
{
    public TypeViewmodel()
    {
        TextBoxMessageTypeViewmodel = "Test - TypeViewmodel";
    }
    public String TextBoxMessageTypeViewmodel { get; set; }
}

的Xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TabControl>
        <TabItem  x:Name="itemtype" Header="ItemType" IsSelected="True" MinHeight="10">
            <Grid DataContext="{Binding TypeViewmodel}">
                <Label Content="{Binding TextBoxMessageTypeViewmodel}"></Label>
            </Grid>
        </TabItem>
        <TabItem Header="ItemCategory"  Margin="-1,0" MinHeight="10" >
            <Grid DataContext="{Binding ItemCategoryViewmodel}">
                <Label Content="{Binding TextBoxMessageItemCategoryViewModel}"></Label>
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

说明: 我创建了两个真正的小视图模型,因此您了解了什么是绑定。 两个内部视图模型都有一个字符串属性,它将在您的选项卡中绑定。 您可以将其替换为您要绑定的任何内容。

在Xaml中,您可以看到TabControl中的每个Grid都绑定到MainViewModel的一个属性,该属性已经是整个View的DataContext(在我的例子中是Window)。 (您可以在TabControl本身中进行绑定)

<Grid DataContext="{Binding TypeViewmodel}">

所以在这一点上,你的DataContext是TypeViewmodel(对于网格内的所有元素!),所以你可以将标签内容绑定到TypeViewModel类的属性:

 <Label Content="{Binding TextBoxMessageTypeViewmodel}"></Label>

这是真正的基本绑定 - 您应该在任何教程中找到它...以及...

希望这会对你有所帮助。

答案 1 :(得分:0)

替换此代码

  this.DataContext = povm;
    this.DataContext = tcvm;

使用此代码

 this.itemtype.DataContext = povm;
 this.ItemCategory.DataContext = tcvm;
相关问题