gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:34:07 25 4
gpt4 key购买 nike

我是WPF的新手,请帮助我。我有两个 View 模型1.ItemCategoryViewModel和2.TypeViewmodel这两个 View 模型来自不同的两个表

<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代码中,我想同时使用两个 View 模型。
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覆盖。我该如何解决,请帮助我

最佳答案

替换此代码

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

用这个代码
 this.itemtype.DataContext = povm;
this.ItemCategory.DataContext = tcvm;

关于c# - 如何使用tabControl在XAML中使用两个ViewModels代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40717284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com