简体   繁体   中英

Change DataGrid Column display value in AutoGeneratingColumn

I have dataGrid.ItemsSource bound to a list of EntityItem, Client , that containt another 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 .

In WindowsForm I could just do :

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

But I can't find a way to do in properly in 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.

DataGridAutoGeneratingColumnEventArgs object has Column property which contains a generated DataGridColumn instance. Concrete type is DataGridTextColumn , which has Binding property.

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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