使用泛型的WPF View/ViewModels-怎么做? [英] WPF View/ViewModels using Generics- how?

查看:60
本文介绍了使用泛型的WPF View/ViewModels-怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF的新手,请多多包涵...

假设我在SQL中有2个表
事情
其他

两者具有完全相同的字段:
ID(int)
名称(字符串)
说明(字符串)
IsActive(位/布尔)
DateModified(DateTime)

因此,我想创建一个模型(而不是两个)并执行以下操作:

New to WPF, so please bear with me...

Suppose I have 2 tables in SQL
Thing
OtherThing

Both have the exact same fields:
ID (int)
Name (string)
Description (string)
IsActive (bit/bool)
DateModified (DateTime)

So, I want to create one Model (not two) and do something like this:

BaseModel<T>()
{
  public int ID {get;set;}
  ...
}


等等.(当然,使用INotifyPropertyChanged,只是试图保持代码简单).

我希望能够创建可以与任何符合Thing/OtherThing的模型一起使用的BaseView和BaseViewModel.

我真的不知该如何使用此通用名称,因此我不必拥有ThingView/ThingViewModel和OtherThingView/OtherThingViewModel ...

看来这应该很简单,但我似乎无法弄清楚.有没有人有一个代码示例,他们可以使用ONE视图,ONE ViewModel和ONE数据模型互换下拉列表中的各种项目?


etc. (of course, using the INotifyPropertyChanged, just trying to keep the code simple).

I want to be able to create a BaseView and BaseViewModel that would work with whatever model conforms to the Thing/OtherThing.

I am really at a loss as to what to do here to make this generic, so I don''t have to have a ThingView/ThingViewModel and a OtherThingView/OtherThingViewModel...

It seems that this should be simple, but I cannot seem to figure it out. Does anyone have a code example where they could interchange various items from a dropdown list using ONE view, ONE ViewModel, and ONE datamodel?

推荐答案

如果两个表具有相同的字段,为什么不每个都使用相同的类呢?无需泛型.

除此之外,我看不出您的问题是什么.您正在尝试做什么以及遇到的具体问题是什么?
If both tables have identical fields, why not just use the same class for each? No need for generics.

Other than that, I don''t see what your problem is. What are you trying to do and what is the specific problem you are running into?


这篇关于使用泛型的WPF View/ViewModels-怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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