将 WPF Infragistics Excel 导出功能与 MVVM 结合使用

发布于 2024-10-16 15:31:48 字数 389 浏览 2 评论 0原文

我正在使用 Infragistics WPF 数据网格,并且我想利用 Infragistics 提供的 Excel 导出功能。用于 Excel 导出的 API 需要网格实例作为参数。这对 MVVM 不太友好,因为我更喜欢处理在 viewModel 中导出数据的命令,而该命令不知道我视图中的 datagrid 控件的实例。

有谁知道使用 Infragistics 库进行 excel 导出(最好基于网格绑定到的集合)的更 MVVM 友好的机制?

I'm using the Infragistics WPF datagrid and I want to make use of the Excel export functionality that Infragistics provides. The API for the Excel export requires an instance of the grid as a parameter. This isn't very MVVM friendly since I'd prefer to handle the command to export the data in my viewModel which wouldn't know about the instance of the datagrid control in my view.

Does anyone know of a more MVVM friendly mechanism to do excel exports (preferable based on the collection the grid is bound to) using the Infragistics libraries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱&纷飞 2024-10-23 15:31:48

如您所知,由于需要引用网格,因此不应将逻辑放入视图模型中。您只需要在代码隐藏中处理导出命令。这样做在概念上并没有什么错误,也没有什么反 MVVM 的地方:这个特定的逻辑恰好是特定于视图的。

Due to the required reference to the grid, as you know, you should not put the logic into the View Model. You just need to handle the Export command in your code-behind. There is nothing conceptually wrong, or anti-MVVM, about doing this: this particular logic just happens to be View-specific.

心清如水 2024-10-23 15:31:48

数据网格是视图的一部分,导出到 Excel 是该网格的一个功能。根本没有 ViewModel 的参与。它是视图活动的纯粹案例。考虑到这是纯视图功能,您不能/不需要在 ViewModel 中处理导出命令,而是在视图(代码隐藏)本身中处理。
明天,如果您将此数据网格替换为另一个数据网格,则导出到 Excel 的功能也将消失,因为它是由 infragistics 提供的。因此你的视图模型不应该做任何事情。但是,如果您想实现不特定于基础设施的通用且独立的功能,您可以在 ViewModel 中实现(因为它是通用的且不依赖于基础设施)。

The datagrid is part of view and export to excel is a feature of this grid. There is no involvement of ViewModel at all. Its a pure case of View activity. Considering this as apure view feature, you cannot / need not handle the Export Command in ViewModel but in the View (code-behind) itself.
Tomorrow if you replace this datagrid with another datagrid, the export to excel feature will also go, since its provided by infragistics. Hence your view model is not supposed to do anything. However if you want to implement a generic and independent functionality which is not specific to infragistics, you may do it in the ViewModel (Because its generic and not dependent of infragistics).

平安喜乐 2024-10-23 15:31:48

简单地说,

您可以像这样在 xaml 中添加按钮,并使用命令参数来传递网格。

<Button Content="Print" HorizontalAlignment="Left" Margin="201,35,0,0" Grid.Row="2"    VerticalAlignment="Top" Width="75" Height="29"
            Command="{Binding PrintCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=ParentC1DataGrid,Mode=OneWay}"
            />

并在您的视图模型中添加以下命令:

private ICommand m_PrintCommand;
    public ICommand PrintCommand
    {

        get {

            if (this.m_PrintCommand == null)
            {
                this.m_PrintCommand = new DelegateCommand<C1.WPF.DataGrid.C1DataGrid>(PrintData);
            }
            return m_PrintCommand;
        }
    }

    private void PrintData(C1.WPF.DataGrid.C1DataGrid v)
    {
////put exporting code here
        PrintDialog printDlg = new PrintDialog();
        printDlg.PrintVisual(v, "Grid Printing.");

    }

simply ,

you can add button in ur xaml like this and use command parmeter to pass grid.

<Button Content="Print" HorizontalAlignment="Left" Margin="201,35,0,0" Grid.Row="2"    VerticalAlignment="Top" Width="75" Height="29"
            Command="{Binding PrintCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=ParentC1DataGrid,Mode=OneWay}"
            />

and in ur view model add this command:

private ICommand m_PrintCommand;
    public ICommand PrintCommand
    {

        get {

            if (this.m_PrintCommand == null)
            {
                this.m_PrintCommand = new DelegateCommand<C1.WPF.DataGrid.C1DataGrid>(PrintData);
            }
            return m_PrintCommand;
        }
    }

    private void PrintData(C1.WPF.DataGrid.C1DataGrid v)
    {
////put exporting code here
        PrintDialog printDlg = new PrintDialog();
        printDlg.PrintVisual(v, "Grid Printing.");

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文