DataTable作为DataGrid.ItemsSource

Wii*_*axx 10 c# wpf datatable binding datagrid

嗨我想将DataTable多列绑定到代码DataGrid隐藏中

    var dt = new DataTable();

    dt.Columns.Add(new DataColumn("1"));
    dt.Columns.Add(new DataColumn("2"));
    dt.Columns.Add(new DataColumn("3"));

    dt.Rows.Add(ff.Mo);
    dt.Rows.Add(ff.Di);
    dt.Rows.Add(ff.Mi);
    dt.Rows.Add(ff.Do);
    dt.Rows.Add(ff.Fr);
    dt.Rows.Add(ff.Sa);
    dt.Rows.Add(ff.So);
// ff is a object that contains List<myCellObj>

DataGrid DGrid = new DataGrid();
for (int i = 0; i < 3; i++)
{
   DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
   templateColumn.HeaderTemplate = HeaderDt;
   templateColumn.CellTemplate = ItemDt; //specified DataTemplate for myCellObj

   DGrid.Columns.Add(templateColumn);
}
Run Code Online (Sandbox Code Playgroud)

现在我该怎样设置我的dt作为ItemsSource,Datacontext或什么都得到它在我View 还,如果你能提供给我直接绑定到我的路Object ff

任何可能有用的东西都非常感激

Lee*_*son 24

假设你在WPF中只是说:

DGrid.ItemsSource = dt.AsDataView();
Run Code Online (Sandbox Code Playgroud)

无需在DataGrid上手动设置列,分配DataTable将为您设置这些列.