我应该为视图使用UserControls而不是DataTemplates吗?

编程入门 行业动态 更新时间:2024-10-21 16:33:43
本文介绍了我应该为视图使用UserControls而不是DataTemplates吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读这篇文章,作者提出了使用DataTemplates定义的建议一个ViewModel是一个疯子的方法(#7).我一直都这样做,真的那么糟糕吗?

I was reading this post and the author makes the suggestion that using DataTemplates to define a ViewModel is a lunatic's way to do it (#7). I do that all the time, is it really that bad?

<DataTemplate DataType="{x:Type local:MyViewModel}"> <Grid> ... </Grid> </DataTemplate>

我的大多数视图只是定义一个或两个DataTemplate的ResourceDictionary.对我来说,这样做比为每个ViewModel创建一个UserControl更好.为什么我不需要WPF的可视树中的多余图层?以及为什么在DataTemplate为我完成视图模型时要照顾将ViewModels映射到Views的问题?这种语法真的是疯子方法"吗?

Most of my Views are simply a ResourceDictionary that defines a DataTemplate or two. To me, it makes much better sense to do this than creating a UserControl for every ViewModel. Why would I want the extra layer in WPF's visual tree when it's not needed? And why would I want to take care of mapping ViewModels to Views when a DataTemplate does that for me? Is this syntax really a "lunatics approach"?

推荐答案

这没什么不好的,除了非常大的xaml文件和缺乏DataTemplate在设计图面上提供的编辑支持之外.

Nothing bad about it, except for incredibly large xaml files and the lack of edit support that DataTemplates have on the design surface.

如果这些问题困扰着您,您可以随时...

If those issues are hurting you, you can always...

<DataTemplate DataType="{x:Type local:MyViewModel}"> <local:MyViewModelUserControl /> </DataTemplate>

更多推荐

我应该为视图使用UserControls而不是DataTemplates吗?

本文发布于:2023-11-25 13:59:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   而不是   UserControls   DataTemplates

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!