[英]How to create DataGridTextColumn of DataGrid dynamically and bind it?
I have two properties: 我有两个属性:
public ObservableCollection<Object> HeaderOfDataGrid {get; set;}
public Observablecollection<Object> BodyOfDataGrid {get; set;}
Is it possible to bind HeaderOfDataGrid
property to DataGridTextColumn
? 是否可以将
HeaderOfDataGrid
属性绑定到DataGridTextColumn
? Yeah, I know that it is possible by binding like that: 是的,我知道可以这样绑定:
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Surname" Binding="{Binding Surname}"/>
<DataGridTextColumn Header="Phone" Binding="{Binding Phone_Number}" />
</DataGrid.Columns>
However, HeaderOfDataGrid
property can have 'Count' equals 700 and I need to show all 700 items of collection HeaderOfDataGrid
in DataGridTextColumns
of DataGrid
. 但是,
HeaderOfDataGrid
属性的“计数”等于700,我需要在DataGridTextColumns
的DataGrid
显示集合HeaderOfDataGrid
所有700个项目。 So, I need to create 700 DataGridTextColumns
based on HeaderOfDataGrid
property(collection). 因此,我需要基于
HeaderOfDataGrid
属性(集合)创建700个DataGridTextColumns
。 Also, if Count
of HeaderOfDataGrid
property is 700, then BodyOfDataGrid
property also has the same Count
(for example, 700). 另外,如果
HeaderOfDataGrid
Count
属性为700,则BodyOfDataGrid
属性也具有相同的Count
(例如700)。 So using hard-coded xaml
is not convenient to me. 因此,使用硬编码的
xaml
对我来说并不方便。
I know I can bind this way: 我知道我可以这样绑定:
<DataGrid ItemsSource="{Binding Path=Body, Mode=TwoWay}"/>
But it is not what I want as I would like to renamed headers located in Header property. 但这不是我想要的,因为我想重命名Header属性中的标头。
How to implement binding and creating without hard-coded xaml
to DataGridTextColumn
? 如何在没有将
xaml
硬编码到DataGridTextColumn
情况下实现绑定和创建?
You can't have it both ways. 您不能同时拥有这两种方式。 If you want to rename the headers, you'll have to code it manually;
如果要重命名标题,则必须手动对其进行编码。 otherwise you do it automatically and use what it gives you.
否则,您会自动执行并使用它所提供的功能。
You may be able to set up a style, where the header is bound to a property of the underlying object. 您可能可以设置样式,其中标题绑定到基础对象的属性。 In that case it may work, but your
BodyOfDataGrid
would have to contain a Header
property 在这种情况下,它可能会起作用,但是您的
BodyOfDataGrid
必须包含Header
属性
What I've found to do is to create DataTable
and bound it to DataGrid
: 我发现要做的是创建
DataTable
并将其绑定到DataGrid
:
XAML: XAML:
<DataGrid Name="dataGrid" />
Code behind: 后面的代码:
public MainWindow()
{
InitializeComponent();
PopulateDataGrid();
}
DataTable employeeDataTable = new DataTable();
private void PopulateDataGrid()
{
var _ds = new DataSet("Test");
employeeDataTable = _ds.Tables.Add("DT");
for (int i = 0; i < 10; i++)//create columns
{
if(i==0)
employeeDataTable.Columns.Add("Row");
employeeDataTable.Columns.Add(i.ToString());
}
for (int i = 0; i < 10; i++)//fill data to rows
{
var theRow = employeeDataTable.NewRow();
for (int j = 0; j < 10; j++)
{
if (j == 0)
{
theRow[j] = (i + 1);
continue;
}
if (j % 2 == 0)
theRow[j] = "a";
else
theRow[j] = "b";
}
employeeDataTable.Rows.Add(theRow);
}
dataGrid.ItemsSource = employeeDataTable.AsDataView();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.