我正在阅读这篇文章,作者提出了使用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吗?
发布评论