Josh史密斯MVVM模式大问题

时间:2010-10-20 03:55:43

标签: wpf mvvm

我正在使用Josh Smith创建项目THE MODEL-VIEW-VIEWMODEL (MVVM) DESIGN PATTER FOR WPF

但有一个问题。同一工作区显示相同的操作我的VIEW包含tabcontrol。在创建2个或更多相同的工作空间后,当我从任何工作空间更改tabcontrol的选定索引时,其他相同的工作空间tabcontrol显示相同的结果。

我在Josh smith示例项目上测试过。但和我的项目一样。如果你想表现出来。从here和MainWindowViewModel.cs下载Josh史密斯代码,改变以下代码

void ShowAllCustomers()
        {
            AllCustomersViewModel workspace = null;
                this.Workspaces.FirstOrDefault(vm => vm is AllCustomersViewModel)
                as AllCustomersViewModel;

            if (workspace == null)
            {
                workspace = new AllCustomersViewModel(_customerRepository);
                this.Workspaces.Add(workspace);
            }

            this.SetActiveWorkspace(workspace);
        }

void ShowAllCustomers()
        {
            AllCustomersViewModel workspace = null;

            workspace = new AllCustomersViewModel(_customerRepository);
            this.Workspaces.Add(workspace);            

            this.SetActiveWorkspace(workspace);
        }

然后运行代码并打开2个或更多All Customer view并更改列表视图列顺序。所有打开的all customer view都显示相同的结果

如何解决此问题

2 个答案:

答案 0 :(得分:3)

如果我可以在http://www.codeproject.com/KB/WPF/CinchV2_3.aspx引用Cinch的创造者Sacha Barber,请参阅“WorkSpaces:Special Notes”部分。听起来你可能会遇到同样的问题,但我也在学习,所以我可能是错的。

“现在所有这些都很棒,但不幸的是WPF在我们的路径中以TabControl的形式引发了一些奇怪。这是一个控件的BASTARD。你们当中有多少人知道在WPF中TabControls VisualTree只保留了VisualTree中的选定项目。

听起来对你不好吗?不,再想一想(虽然这只是使用DataTemplates时的一个问题,直接TabItem / View组合是可以的)。所以我们有几个使用MeffedMVVM在TabControl中创建ViewModel的视图。然后我们更改选项卡,猜测View被删除的内容,当我们回到之前的TabItem时,正如我们使用view 1st和MeffedMVVM一样,为View创建了一个新的ViewModel。“

答案 1 :(得分:1)

我不确定我是否理解您要说的内容,但我遇到的问题是在添加工作区时未选择新选项卡。在TabControl上将IsSynchronisedWithCurrentItem属性设置为True解决了我的问题。