真正的MVVM和第三方控件 [英] True MVVM and third party controls

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

问题描述

在True MVVM模型中,我们不希望在xaml.cs中包含任何代码,也不希望viewModel具有引用视图. 但是,所有第三方控件均未提供对True MVVM的良好支持.

In a True MVVM model we do not expect any code behind in xaml.cs also we do not expect viewModel to have refernece of view. However all third party controls do not provide good support for True MVVM.

就我而言,我正在使用Infragistics xamDatagrid控件,并且希望将其数据导出到excel.我可以将数据导出到Excel的最佳方法是使用以下代码:

In my case I am using Infragistics xamDatagrid control and I want to export its data to excel. The only way I can export data to excel of data grid is by using following code:

xamDataGridExcelExporter.xamDataGridExcelExporter xamDataGridExcelExporter1 =       
   new xamDataGridExcelExporter.xamDataGridExcelExporter();   
xamDataGridExcelExporter1.Export(**this.xamDataGrid1**,   
   @"C:\Excel\ExportFile.xls");

但是,XamDataGridExcelExporter将输入作为this.xamDataGrid. xamDataGrid是视图而非​​viewModel的一部分. 所以我们该如何处理在viewModel中需要视图实例的情况.

However, XamDataGridExcelExporter takes input as this.xamDataGrid. xamDataGrid is part of View not viewModel. So how can we handle such kind of cases where we need instance of view in viewModel.

推荐答案

您可以围绕xamDataGrid编写一个包装,该包装具有名为filename的依赖项属性.然后,视图模型可以绑定到该属性.当xamDataGrid检测到filename属性的更改时,它可以执行您建议的代码.然后重置filename属性以进一步通知.

You can write a wrapper around xamDataGrid that has a dependencyproperty called filename. The viewmodel can then bind to this property. When the xamDataGrid detects a change on the filename property it can then execute the code you suggested. Afterwards reset the filename property for further notification.

此解决方案将代码从您的代码后面隐藏下来,并使xamDataGrid负责导出其数据.

This solution keeps out the code from you code behind and makes the xamDataGrid responsible for exporting its data.

-------编辑---------

-------edit---------

第二种解决方案可以利用MVVM照明使者类.而不是声明依赖项属性,而是使包装器侦听消息.当视图模型发送消息时(例如,可以使用文件名作为参数),包装程序可以执行代码.

A second solution can make use of the MVVM light messenger class. In stead of declaring a dependency property, make your wrapper listen to a message. When the viewmodel sends the message (which could for example have the filename as parameter) the wrapper can then execute the code.

例如

public class ExportableXamDataGrid: XamDataGrid
{
    public ExportableXamDataGrid():base()
    {
        Messenger.Default.Register<string>(this,"ExportExcel",ExportFile);
    }

    private void ExportFile(string file)
    {
        xamDataGridExcelExporter.xamDataGridExcelExporter xamDataGridExcelExporter1 =       
        new xamDataGridExcelExporter.xamDataGridExcelExporter();   
        xamDataGridExcelExporter1.Export(**this.xamDataGrid1**,   
           @"C:\Excel\ExportFile.xls");

    }
}

然后在您的视图模型中可以执行以下操作:

Then in your viewmodel you can do:

 Messenger.Default.Send(@"C:\Excel\ExportFile.xls","ExportExcel");

有很多解决问题的方法,而不必全部在视图中开始编写逻辑.

There are many solutions to your problem, all of which you do not have to start writing logic in your view.

http://www.lucbos.net/2011/06/using-codebehind-in-mvvm.html

这篇关于真正的MVVM和第三方控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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