通过名称设置 ContentTemplate 与通过类型推断设置 ContentTemplate 的区别

发布于 2024-10-20 07:06:02 字数 1228 浏览 1 评论 0原文

最近开始学习WPF + MVVM。有可能我的做法是错误的,或者完全没有按照应该做的事情去做。

我正在尝试创建一个简单的订单应用程序,它有订单项目,每个订单都有订单项目。主页上有两个部分(首先它们都是用户控件)。

顶部绑定到订单列表。底部绑定到当前选定的订单(其想法是它将显示该订单的详细信息)。

详细信息视图需要能够查看/编辑和添加新项目。为此,我认为我应该有两个视图/模板,编辑订单视图和新订单视图/模板。在阅读过程中,我遇到了数据模板,并认为这将是实现这一目标的好方法。

在我的 mainwindow.xaml 中,我有订单视图列表,如下所示:

<ContentControl Content="{Binding Path=OrdersViewModel}"

该视图在资源文件中与以下内容挂钩:

<DataTemplate DataType="{x:Type vm:AllOrdersViewModel}">
    <vw:AllOrdersView />
</DataTemplate>

这非常有效,当应用程序启动时,它们会显示订单。我对订单详细信息进行了同样的操作(即,它只设置了数据类型,而不定义任何键。这效果很好,如果没有选择订单,则视图将不会显示,当选择订单时,它将显示 我目前正在尝试将其更改为模板,

以便我可以动态切换模板/视图,以便当用户单击新订单时(视图模型有一个枚举来说明订单的当前状态,因此我会检查.枚举值),

如果我尝试以下操作,应用程序将启动,显示订单列表(此时未选择订单),并显示详细信息视图,然后显示其中之一。转换器将由于 dependencyproperty.unsetvalue 而爆炸(附注,我应该始终检查这一点吗?理想情况下,我不希望在选择订单之前显示视图:

<ContentControl Content="{Binding Path=OrderViewModel}" ContentTemplate="{StaticResource ordertemplate}" />

   <DataTemplate x:Key="ordertemplate">
       <TextBlock Text="hello" />
    </DataTemplate>

我主要有两个问题:

1)我会如何做?在有数据要显示之前停止显示模板?

2)如何根据枚举动态切换视图/模板?

I have recently start to learn WPF + MVVM. It is possible I have the wrong end of the stick or the wrong stick completely with the way things should be done.

I am trying to create a simple order app, it has order items, each order has lineitems. There are two sections on the main page, (to start with they were both usercontrols).

The top is bound to the list of orders. The bottom is bound to the currently selected order, (the idea being it will display the detail of that order).

The details view needs to be able to view / edit and add new items. To this end I thought I should have two views / templates, the edit order view and the new order view / template. During my reading I came across datatemplates and thought this would be a good way to achieve this.

In my mainwindow.xaml I have the list of orders view bound as follows:

<ContentControl Content="{Binding Path=OrdersViewModel}"

The view is hooked up in the resources file with:

<DataTemplate DataType="{x:Type vm:AllOrdersViewModel}">
    <vw:AllOrdersView />
</DataTemplate>

This works great, when the app starts up they orders are displayed. I did the same with the order detail to start with (i.e. it only had the datatype set rather than any key defined. This worked well, if no order was selected then the view would not display, when an order was selected it would show up.

I am currently trying to change this to a template so that I can dynamically switch the template / view so that when they user clicked NEW order (the viewmodel has an Enum that states the current state of an order, so I would check against the enum value), the view should change.

If I try the following it will blow up. The app starts, the list of orders is displayed (at this point no orders are selected), and the details view is displayed, then one of the converters will blow up due to dependencyproperty.unsetvalue, (side note, should I always check for that?). Ideally I do not want the view to be shown until an order is selected:

<ContentControl Content="{Binding Path=OrderViewModel}" ContentTemplate="{StaticResource ordertemplate}" />

   <DataTemplate x:Key="ordertemplate">
       <TextBlock Text="hello" />
    </DataTemplate>

I have two questions mainly:

1) how would I stop the template from being displayed until it has data to display?

2) how could I switch the views / templates dynamically based on an enum?

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

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

发布评论

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

评论(1

智商已欠费 2024-10-27 07:06:02

对于这种情况,最直接的机制是 DataTemplateSelector,它包含您编写的代码,可以检查绑定对象(在本例中为 ViewModel),并选择要使用的适当模板,包括处理没有详细选择的情况。

<ContentControl Content="{Binding Path=OrderViewModel}" 
                ContentTemplateSelector="{StaticResource MyCustomSelector}" />

The most straightforward mechanism to use for this situation would be a DataTemplateSelector, which contains code that you write that can examine the bound object, in this case your ViewModel, and choose the appropriate template to use, including handling the case of no detail selection.

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