问题描述
我有 dataGrid.ItemsSource
绑定到EntityItem列表 Client
,其中包含另一个EntityItem Company
.
I have dataGrid.ItemsSource
bound to a list of EntityItem, Client
, that containt another EntityItem, Company
.
当显示我的 dataGrid
时,在我的 Company
列中,我具有对象的类型( System.Data.Entity.
...)我想显示我的 Company.Name
.
When my dataGrid
is displayed, in my Company
Column, I have the type of my object (System.Data.Entity.
...) I would like instead to display my Company.Name
.
在WindowsForm中,我可以这样做:
In WindowsForm I could just do :
e.Value = ((Company)(dgv["Company", e.RowIndex].Value)).Name;
但是我找不到在WPF中正确执行操作的方法.
But I can't find a way to do in properly in 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
,但这仅是为了获取属性.
So I can make sure I'm on the right column, but now I'm stuck, I don't know how to change the way I want the column to display the data ...I tried to look into e.PropertyDescriptor
but it's only to Get the properties.
推荐答案
DataGridAutoGeneratingColumnEventArgs
对象具有 Column
属性,该属性包含一个生成的 DataGridColumn
实例.具体类型为 DataGridTextColumn
,它具有 Binding
属性.
DataGridAutoGeneratingColumnEventArgs
object has Column
property which contains a generated DataGridColumn
instance. Concrete type is DataGridTextColumn
, which has Binding
property.
您可以更改绑定路径以使用 Column.Name
属性
You can change binding path to work with Column.Name
property
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");
}
}
这篇关于更改AutoGeneratingColumn中的DataGrid列显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!