gpt4 book ai didi

wpf - 使用 MVVM 模式的 WPF 应用程序的 DataTemplate 将 View 链接到 ViewModel

转载 作者:行者123 更新时间:2023-12-01 13:00:08 25 4
gpt4 key购买 nike

目前我有一些 View 使用类似于以下的代码链接到 ViewModels:

<Application.Resources>
<DataTemplate DataType="{ x:Type vm:AgeIndicatorViewModel}">
<v:AgeIndicatorView />
</DataTemplate>
</Application.Resources>

我有两个问题:

  • 此方法是否允许我仅将一个 View 链接到每个 View 模型(我认为它确实对我施加了此限制,但想确定一下)

  • 使用此方法时,我应该将所有 DataTemplate 声明放在哪里?目前只有少数几个,它们都在 App.Xaml 中 - 是否有更好的位置,或者 App.Xaml 是否合适/最佳位置?

最重要的问题确实是第二个,因为目前我以这种方式将我的 ViewModel 链接到我的 View,因为它不需要外部库等。

我的 ViewModels 的设置方式、它们的属性和命令等都已经可以正常工作了。

最佳答案

Does this method allow me to only link one View to each View Model (I think it does improse this limitation on me, but want to be sure)

是的。如果您尝试将多个 ViewModel 链接到多个 View ,您需要将它们封装在一个单独的 VM 中,并添加一个新的 DataTemplate。

When using this method, where should I put all of my DataTemplate declarations? At the moment there are only a few, and they are all in App.Xaml - Is there a better location for these, or is App.Xaml fine / Best location?

App.Xaml 很好,或者实际上是可视层次结构中将使用 DataTemplate 的任何地方。

话虽这么说,如果项目成为一个非常大规模的项目,开始使用 Merged Resource Dictionaries 通常会更好。 - 这允许您在定义 View/ViewModel 对的位置“附近”设置资源字典,然后在更高级别使用它们(即:将它们合并到 App.Xaml 中)。

关于wpf - 使用 MVVM 模式的 WPF 应用程序的 DataTemplate 将 View 链接到 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6784091/

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