如何使用 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
How to programmatically add columns to wpf datagrid with MVVM?
提问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.
答案很简单。
- Define a
DataTablein the view model - Define columns (In my case I had to define columns
programmaticallywithin aforeachloop) - Add rows
- Then bind the DataTable for the
ItemsSourceproperty of the datagrid. (Make sure that theAutoGeneratedColumns=True)
DataTable在视图模型中定义一个- 定义列(在我的情况下,我必须
programmatically在foreach循环中定义列) - 添加行
- 然后为数据
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>

