2016-06-09 46 views
0

我已将dataGrid.ItemsSource绑定到EntityItem的列表Client,后者包含另一个EntityItem,Company在AutoGeneratingColumn中更改DataGrid列显示值

当显示我的dataGrid,在我Company栏,我有我的目标(System.Data.Entity. ...)的类型,我想,而不是显示我的Company.Name

在WindowsForm我可能只是这样做:

e.Value = ((Company)(dgv["Company", e.RowIndex].Value)).Name; 

但我不能找到一种方法,在WPF中做正确。

现在我有:

private void dataGridUsers_AutoGeneratingColumn_1(object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 

     DataGrid dgv = (DataGrid)sender; 
     if (e.PropertyName == "Company") 
     { 
      if (e.PropertyType == typeof(Company)) 
      { 
       ... 
      } 
     } 
    } 

所以我可以确保我在右列,但现在我坚持,我不知道如何改变我想要的列的方式以显示数据... 我试图查看e.PropertyDescriptor,但它只是为了获取属性。

回答

1

DataGridAutoGeneratingColumnEventArgs对象有Column属性其中包含生成的DataGridColumn实例。具体类型为DataGridTextColumn,其中有Binding属性。

可以更改绑定路径与Column.Name物业工作

private void DataGridOnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 
    if (e.PropertyName == "Company") 
    { 
     var c = (DataGridTextColumn)e.Column; 
     var b = (Binding)c.Binding; 
     b.Path = new PropertyPath("Company.Name"); 
    } 
}