如何在 MVVM 中使用两个用户控件创建主从视图?

发布于 2024-09-30 02:00:49 字数 1213 浏览 1 评论 0 原文

我对如何使用两个不同的用户控件创建主详细信息视图有点困惑。

有三种选择,

选择 1

CustomerMasterView + CustomerMasterViewModel
CustomerDetailView + CustomerDetailViewModel

并将两个视图模型保留在 App.Resources 中,

但是通过这样做,与所有静态资源源标记代码的绑定变得复杂。

选择 2

CustomerViewModel
CustomerMasterView
CustomerDetailView

两个视图通过 App.Resources 共享相同的 ViewModel,即使绑定代码有太多项目也是如此。

选择 3

CustomerMasterView + CustomerMasterViewModel
CustomerDetailView + CustomerDetailViewModel

两个视图都将 DataContext 设置为其相应的 ViewModel。现在这是一个小问题, CustomerMasterView 有选择器(ListBox 或 DataGrid 或其他),其 SelectedItem 需要作为双向绑定绑定到 CustomerDetailViewModel 的“Customer”属性。

看起来不错吗?

<!-- CustomerMasterView -->

<ListBox
    ItemsSource="{Binding CustomerList}"
    SelectedItem="{Binding DataContext.Customer,ElementName=customerDetailView}"
    />

<local:CustomerDetailView
    x:Name="customerDetailView"
    />

但通过这样做,我违背了 ViewModel 的目的,因为它在我的 UI 代码中添加了更多依赖项。

哪一种是最优选的方式还是还有其他方式? 我应该创建嵌套视图模型吗?

我也在尝试学习 Prism,并且我对如何正确执行此操作没有什么困惑,任何帮助都会得到帮助。

I am little confused about how to create Master Detail view with two different user controls.

There are three choices,

Choice 1

CustomerMasterView + CustomerMasterViewModel
CustomerDetailView + CustomerDetailViewModel

And keep both View Models in App.Resources

But by doing this, binding becomes complex with all static resources source markup code.

Choice 2

CustomerViewModel
CustomerMasterView
CustomerDetailView

Both views share same ViewModel via App.Resources, well even with the binding code has too many items.

Choice 3

CustomerMasterView + CustomerMasterViewModel
CustomerDetailView + CustomerDetailViewModel

Both view's have DataContext set to their corresponding ViewModel. Now here is the little issue,
CustomerMasterView has Selector (ListBox or DataGrid or whatever), whose SelectedItem needs to be bound to CustomerDetailViewModel's "Customer" Property as two way binding.

Does it look good?

<!-- CustomerMasterView -->

<ListBox
    ItemsSource="{Binding CustomerList}"
    SelectedItem="{Binding DataContext.Customer,ElementName=customerDetailView}"
    />

<local:CustomerDetailView
    x:Name="customerDetailView"
    />

But by doing this, I am defying purpose of ViewModel as it adds more dependency in my UI code.

Which one is most preferred way or is there any other way?
Should I create nested View Models?

I am also trying to learn Prism, and I have little confusion of how to do this right, any help will be appriciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情归归情 2024-10-07 02:00:49

我认为最简单的方法是拥有一个带有两个用户控件的视图模型。

您的 ViewModel 将包含您的客户列表,以及“ActiveCustomer”或“SelectedCustomer”。您的 MasterView(DataContext 设置为您的 ViewModel)将包含您的列表,连接如下:

您的 MasterView 还将包括您的 DetailsView (另一个 UserControl)。它看起来像这样:

The simplest appraoch in my opinion would be to have a single view model, with two UserControls.

Your ViewModel would have your list of customers, as well as a "ActiveCustomer" or "SelectedCustomer". Your MasterView (with the DataContext set to you ViewModel), would contain your list, wired up like this:

<ListBox ItemsSource="{Binding Customers}" SelectedItem="{Binding ActiveItem, Mode=TwoWay}" />

Your MasterView will also include your DetailsView (another UserControl). It looks like this:

<views:DetailsUserControl DataContext="{Binding ActiveItem}" />

笑,眼淚并存 2024-10-07 02:00:49

You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It shows how to implement a Master/Detail scenario with two Views (separate UserControls) and two ViewModels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文