Search code examples
wpfdatagrid

Get Item from DataGridCellInfo in WPF


I have a DataGrid in WPF. The DataGridCellInfo contains Column, IsValid and Item.

private void ProjectsDatagrid_SelectionChanged(object sender, MouseButtonEventArgs e) {
            var selected = ProjectsDatagrid.SelectedCells[0];
        }

I tried:

var foo = selected.Item.Name

var foo2 = selected.Item[Name]

How am I supposed to get the value from the field Name?


Solution

  • Cast Item to your type, e.g.:

    var dataObject = selected.Item as YourClass;
    if (dataObject != null)
    {
        string name = dataObject.Name;
        //...
    }
    

    ...where YourClass is a custom class with a Name property.