简体   繁体   English

WPF MVVM 和视图 inheritance

[英]WPF MVVM and View inheritance

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}"

Note that the only real differences between these to blocks are the names of the bindings used (Person vs Car).请注意,这些与块之间唯一真正的区别是使用的绑定的名称(Person vs Car)。

I was thinking of maybe creating one BaseView class that the other views inherit from.我正在考虑创建一个 BaseView class 其他视图继承自。 This base class would use generic enough binding names so that it can be reused, such as:这个基础 class 将使用足够通用的绑定名称,以便可以重用,例如:

<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}"

This way, my PersonsView and CarsView can inherit from BaseView and that's it.这样,我的 PersonsView 和 CarsView 可以从 BaseView 继承,仅此而已。 I would also have to make changes to the ViewModels though, so that they expose the correctly named properties, such as DataItem.不过,我还必须对 ViewModel 进行更改,以便它们公开正确命名的属性,例如 DataItem。 I guess I could create a base ViewModel interface that exposes the desired properties and have the other ViewModels implement that.我想我可以创建一个基本的 ViewModel 接口来公开所需的属性并让其他 ViewModel 实现它。

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?如我所述,尝试创建基本视图或基本视图 model 是不是一个坏主意?

Thanks.谢谢。

You're really going to create the inheritance in your view models, not your views.你真的要在你的视图模型中创建 inheritance,而不是你的视图。 I'd define an ItemViewModelBase class that exposes ItemName , Items , and SelectedItemName properties and derive my view models from it.我将定义一个ItemViewModelBase class 公开ItemNameItemsSelectedItemName属性并从中派生我的视图模型。

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.实际上,除非您需要在视图中进行自定义,否则您不需要多个视图:您只需要一个呈现ItemViewModelBase对象的视图。

Of course, if you do need the views to be different, you can do a certain amount of customization, eg:当然,如果您确实需要不同的视图,您可以进行一定程度的自定义,例如:

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

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() .现在,如果您不提供数据模板,则每当 WPF 呈现 object 时,它都会创建一个包含object.ToString()TextBlock Implementing a generic base class gives you a way to globally override this behavior just by creating one data template, eg:实现通用基础 class 为您提供了一种仅通过创建一个数据模板来全局覆盖此行为的方法,例如:

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

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.这并不比重写ToString()以返回ItemName (这是我开始的地方)更容易,但是如果(例如)您想要一个在用户将鼠标悬停在其上时显示详细信息的ToolTip ,您只需将其添加到这个模板,它可以在您的 UI 中随处使用。

May be you can continue having one generic view model, but having, instead, multiple data layers.可能您可以继续拥有一个通用视图 model,但拥有多个数据层。 This can basically help you to push complexity on data layer,which is basically easier to test and debug.这基本上可以帮助您在数据层上推复杂度,这基本上更容易测试和调试。 But everything is too context dependent.但是一切都太依赖上下文了。 Good luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM