继承/“组合化”在 WPF 视图中
假设我的 WPF 应用程序中有多个视图,它们都显示一个报告,但每个视图显示不同的报告。
我创建了一个基本 ViewModel,其中包含所有报告共有的所有内容。每个报表视图都有自己的 ViewModel,该 ViewModel 派生自基本 ViewModel。这些派生的 ViewModel 具有以下职责:
- 生成报告数据
- 格式化报告数据以进行打印
- 提供报告参数的属性
- 提供可在生成的报告上执行的其他操作的命令(可选)
这一切都很好且简洁,但是,视图一团糟。 基本上,每个视图都是相同的,只有一些细微的变化,例如:
- 为报告参数提供不同的控件
- 绑定到命令以执行附加操作的按钮
我想实现以下目标:
- 提供 一个基本视图,定义所有报表视图共有的所有内容,类似于 ASP.NET 中的 Site.master。这将包含搜索按钮、打印按钮、报告显示的网格等...
- 具有具体视图 - 每个报告一个 - 仅定义搜索参数的控件和附加操作的按钮
如何做这?谷歌搜索 WPF 母版页会带来很多定制的解决方案 - 肯定有一个标准的方法吗?
Assume I have several Views in my WPF application that all show a report, but each View shows a different report.
I created a base ViewModel that contains all the stuff all reports have in common. Each report View gets its own ViewModel that is derived from the base ViewModel. These derived ViewModels have the following responsibilities:
- Generate the Report Data
- Format the Report Data for printing
- Provide properties for the report parameters
- Provide Commands for additional actions that can be executed on the generated report (optional)
That's all fine and concise, however, the Views are a complete mess.
Basically, each View is the same with only some minor changes, for example:
- Provide different controls for the report parameters
- Provide buttons that bind to the Commands for the additional actions
I would like to achieve the following:
- Have a base View that defines all the stuff all report Views have in common, similar to a Site.master in ASP.NET. This would contain the search button, the print button, the grid the report is shown in etc...
- Have concrete Views - one for each report - that defines only the controls for the search parameters and the buttons for the additional actions
How to do this? Googling for WPF master page brings up a lot of custom made solutions - surely there must be a standard way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 WPF 中,如果将
Content
设置为相同的,则可以根据其
实例。DataTemplate.DataType
自动设置占位符ContentControl.ContentTemplate
DataTypehttp://www.japf.fr/2009/ 03/思考-with-mvvm-data-templates-contentcontrol/
In WPF, a placeholder
ContentControl.ContentTemplate
can be set automatically based on itsDataTemplate.DataType
if theContent
is set to that sameDataType
instance.http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/
我不确定这是创建复合视图的标准方法,但我已经成功定义了一个主视图(在我的例子中是一个 Window ),其中包含一个系列所需的公共控件视图,并使用
ContentControl
作为 ASP 中的ContentPlaceholder
的等效项,为每个不同的子视图注入不同的UserControl
到主视图中.NET 母版页。就我而言,我定义了一个所有子视图都实现的接口:这允许我的
ApplicationController
在处理显示特定视图的请求时将视图模型推送到子视图中。然后,在显示之前,子视图通过主视图上的 setter 属性与主视图组合。ApplicationController
是集中打开和关闭视图任务的类;每当应用程序中的任何内容想要显示特定视图时,它都会询问ApplicationController
。当它收到显示特定视图的请求时,它会从内部“注册表”查找并构造相应的子视图和视图模型子类,并将各个部分组合在一起。在应用程序启动时,您创建ApplicationController
并注册 ViewModel 子类和 View 子类的组合。部分示例实现是:在 shell 的
Display
方法(在 IShellView 接口上定义)中,您将执行以下操作:应用程序启动将包括以下内容:
I'm not sure that this is the standard way to create composite views, but I've had some success defining a master view (a
Window
in my case) that contains the common controls needed by a family of views and injecting a differentUserControl
for each different sub-view into the master by using aContentControl
as the equivalent to aContentPlaceholder
in an ASP.NET masterpage. In my case, I've defined an interface that all sub-views implement:This allows my
ApplicationController
to push the view model into the child view when handling a request to show a particular view. The sub-view is then composed with the master view via a setter property on the master view before being shown.The
ApplicationController
is the class that centralizes the tasks of opening and closing views; whenever anything in the application wants to show a particular view it asks theApplicationController
. When it receives a request to show a particular view it looks up and constructs the corresponding sub-view and View Model sub-class from an internal "registry", and composes the parts together. At application start-up you create theApplicationController
and register the combinations of ViewModel subclass and View subclass. A partial example implementation is:In the shell's
Display
method (defined on the IShellView interface) you'd do something like:The application start-up would include something like: