WPF MVVM和查看继承 [英] WPF MVVM and View inheritance

查看:166
本文介绍了WPF MVVM和查看继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有十几种不同的意见,这是除了它们绑定到属性的名称几乎相同。例如,下面的部分是形成两种不同的看法:

I have about a dozen different views, which are pretty much identical except for the names of the properties they bind to. For example, the below sections are form two different views:

<TextBlock Text="{Binding PersonName}">       
<GroupBox Header="{Binding PersonName}">
  <ComboBox Text="{Binding SelectedPersonName}" SelectedItem="{Binding SelectedPerson}" ItemsSource="{Binding People}" DisplayMemberPath="PersonName"/>
</GroupBox>
<igDP:XamDataGrid DataSource="{Binding PersonEntries}"


<TextBlock Text="{Binding CarName}">       
<GroupBox Header="{Binding CarName}">
  <ComboBox Text="{Binding SelectedCarName}" SelectedItem="{Binding SelectedCar}" ItemsSource="{Binding Cars}" DisplayMemberPath="CarName"/>
</GroupBox>
<igDP:XamDataGrid DataSource="{Binding CarEntries}"

请注意,只有要阻止这些之间的真正区别是使用的绑定(人VS汽车)的名称。

Note that the only real differences between these to blocks are the names of the bindings used (Person vs Car).

我想也许制作一个基本视角类的其他视图继承的。这个基类将使用通用的足够约束力的名称,以便它可以重复使用,如:

I was thinking of maybe creating one BaseView class that the other views inherit from. This base class would use generic enough binding names so that it can be reused, such as:

<TextBlock Text="{Binding DataItemName}">       
<GroupBox Header="{Binding DataItemName}">
  <ComboBox Text="{Binding SelectedDataItemName}" SelectedItem="{Binding SelectedDataItem}" ItemsSource="{Binding DataItems}" DisplayMemberPath="DataItemName"/>
</GroupBox>
<igDP:XamDataGrid DataSource="{Binding DataItemEntries}"

这一句,我的PersonsView和CarsView可以从基本视点继承,就是这样。我也不得不做出改变的ViewModels不过,让他们暴露正确命名的属性,如DataItem的。我想我可以创建一个公开所需的性能,并有其他的ViewModels实施基本视图模型接口。

This way, my PersonsView and CarsView can inherit from BaseView and that's it. I would also have to make changes to the ViewModels though, so that they expose the correctly named properties, such as DataItem. I guess I could create a base ViewModel interface that exposes the desired properties and have the other ViewModels implement that.

任何上述看法?难道是一个坏主意去尝试,因为我所描述的创建基础视图或基本视图模式?

Any thoughts on the above? Would it be a bad idea to try to create a base view or base view model as I described?

感谢。

推荐答案

您真的要在您的视图模型创建的继承,而不是你的看法。我定义了一个 ItemViewModelBase 类,它暴露了 ITEMNAME 项目 SelectedItemName 属性并从中获得我的视图模型。

You're really going to create the inheritance in your view models, not your views. I'd define an ItemViewModelBase class that exposes ItemName, Items, and SelectedItemName properties and derive my view models from it.

自己真的不继承本身的意见。事实上,除非你在视图需要定制,你不需要多个视图:你只需要一个观点,即呈现 ItemViewModelBase 的对象。

The views themselves don't really "inherit" per se. In fact, unless you need customization in the view, you don't need multiple views: you only need one view that presents ItemViewModelBase objects.

当然,如果你确实需要的看法有所不同,你可以做一定量的定制,例如:

Of course, if you do need the views to be different, you can do a certain amount of customization, e.g.:

<DataTemplate DataType="{x:Type CarsViewModel}">
   <DockPanel>
      <Label DockPanel.Dock="Top">Cars</Label>
      <local:ItemView/>
   </DockPanel>
</DataTemplate>

这是另一个原因,一个很酷的想法。现在,如果你不提供数据模板,每当WPF提供了一个对象,它创建了一个的TextBlock 包含 object.ToString()。实现一个通用基类为您提供了一种方式在全球范围内通过创建一个数据模板,如覆盖此行为:

This is a cool idea for another reason. Right now, if you don't provide a data template, whenever WPF presents an object it creates a TextBlock containing object.ToString(). Implementing a generic base class gives you a way to globally override this behavior just by creating one data template, e.g.:

<DataTemplate DataType="{x:Type ItemViewModelBase}">
   <TextBlock Text="{Binding ItemName}"/>
</DataTemplate>



这不是不仅仅是覆盖的ToString()返回 ITEMNAME (这是我开始),但如果(比如)你想有一个工具提示显示上方时用户鼠标,你只需将其添加到这一个模板,它的工作原理无处不在UI的详细信息。

That's not easier than just overriding ToString() to return ItemName (which is where I'd start), but if (for instance) you want a ToolTip that displays detailed information when the user mouses over it, you just add it to this one template and it works everywhere in your UI.

这篇关于WPF MVVM和查看继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆