如何使用 MVVM 以编程方式将列添加到 wpf 数据网格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15755838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:25:24  来源:igfitidea点击:

How to programmatically add columns to wpf datagrid with MVVM?

wpfmvvmdatagrid

提问by Haritha

I wanna create a pivot table in the user interface in a WPF with MVVM application. So the number of columns are not static.

我想在带有 MVVM 应用程序的 WPF 中的用户界面中创建一个数据透视表。所以列数不​​是静态的。

I have found that I can programmatically add columns from the code behind file (as shown in below code snippets).

我发现我可以以编程方式从文件背后的代码中添加列(如下面的代码片段所示)。

myDataGrid.Columns.Add(column );

But I don'twanna use the code behind file. I wanna do this with MVVM (from the view model). Can anyone give me a solution for this?

但我想要使用的代码隐藏文件。我想用 MVVM(从视图模型)来做到这一点。谁能给我一个解决方案?

采纳答案by Haritha

I found the solution.

我找到了解决方案。

The Answer is simple.

答案很简单

  1. Define a DataTablein the view model
  2. Define columns (In my case I had to define columns programmaticallywithin a foreachloop)
  3. Add rows
  4. Then bind the DataTable for the ItemsSourceproperty of the datagrid. (Make sure that the AutoGeneratedColumns=True)
  1. DataTable在视图模型中定义一个
  2. 定义列(在我的情况下,我必须programmaticallyforeach循环中定义列)
  3. 添加行
  4. 然后为数据ItemsSource网格的属性绑定数据表。(确保AutoGeneratedColumns=True

My View Model

我的 View Model

private DataTable sizeQuantityTable;

public DataTable SizeQuantityTable
    {
        get
        {
            return sizeQuantityTable;
        }
        set
        {
            sizeQuantityTable = value;
            NotifyPropertyChanged("SizeQuantityTable");
        }
    }

I have assinged dummy datain the constructor

我已经dummy data在构造函数中分配了

public MainViewModel()
{
        this.SizeQuantityTable = new DataTable();

        DataColumn sizeQuantityColumn = new DataColumn();
        sizeQuantityColumn.ColumnName = "Size Quantity";
        this.SizeQuantityTable.Columns.Add(sizeQuantityColumn);

        DataColumn sColumn = new DataColumn();
        sColumn.ColumnName = "S";
        this.SizeQuantityTable.Columns.Add(sColumn);

        DataColumn mColumn = new DataColumn();
        mColumn.ColumnName = "M";
        this.SizeQuantityTable.Columns.Add(mColumn);

        DataRow row1 = this.SizeQuantityTable.NewRow();
        row1[sizeQuantityColumn] = "Blue";
        row1[sColumn] = "12";
        row1[mColumn] = "15";
        this.SizeQuantityTable.Rows.Add(row1);

        DataRow row2 = this.SizeQuantityTable.NewRow();
        row2[sizeQuantityColumn] = "Red";
        row2[sColumn] = "18";
        row2[mColumn] = "21";
        this.SizeQuantityTable.Rows.Add(row2);

        DataRow row3 = this.SizeQuantityTable.NewRow();
        row3[sizeQuantityColumn] = "Green";
        row3[sColumn] = "24";
        row3[mColumn] = "27";
        this.SizeQuantityTable.Rows.Add(row3);

        DataRow row4 = this.SizeQuantityTable.NewRow();
        row4[sizeQuantityColumn] = "Yellow";
        row4[sColumn] = "30";
        row4[mColumn] = "33";
        this.SizeQuantityTable.Rows.Add(row4);
    }

My view

我的 view

<Window x:Class="Pivot.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:Pivot.ViewModels"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <Grid.DataContext>
            <vm:MainViewModel />
        </Grid.DataContext>

        <DataGrid 
            ItemsSource="{Binding SizeQuantityTable}"
            AutoGenerateColumns="True"
            />

    </Grid>
</Window>