简体   繁体   English

更改AutoGeneratingColumn中的DataGrid列显示值

[英]Change DataGrid Column display value in AutoGeneratingColumn

I have dataGrid.ItemsSource bound to a list of EntityItem, Client , that containt another EntityItem, Company . 我有dataGrid.ItemsSource绑定到EntityItem, Client的列表,该列表包含另一个EntityItem, Company

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 . 当显示我的dataGrid ,在“ Company列中,我具有对象的类型( System.Data.Entity. 。...),而我想显示我的Company.Name

In WindowsForm I could just do : 在WindowsForm中,我可以做:

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

But I can't find a way to do in properly in WPF. 但是我找不到在WPF中正确执行操作的方法。

For now I have : 现在我有:

private void dataGridUsers_AutoGeneratingColumn_1(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {

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

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. 所以我可以确保我在正确的列上,但是现在我被卡住了,我不知道如何更改我希望该列显示数据的方式...我试图查看e.PropertyDescriptor但是只是获取属性。

DataGridAutoGeneratingColumnEventArgs object has Column property which contains a generated DataGridColumn instance. DataGridAutoGeneratingColumnEventArgs对象具有Column属性,该属性包含一个生成的DataGridColumn实例。 Concrete type is DataGridTextColumn , which has Binding property. 具体类型为DataGridTextColumn ,它具有Binding属性。

You can change binding path to work with Column.Name property 您可以更改绑定路径以使用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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM