简体   繁体   中英

Get selected cell from datagrid with templatecolumns

We have an observable collection that consists of a custom class called Row which is used as itemsource in our Datagrid. The Row class itself consists of 3 different cell types.

public class Row
{

    public TimeCell TimeCell { get; set; }
    public PositionsCell PositionsCell { get; set; }
    public TemperatureCell TempCell { get; set; }
}

These cell types all have the parent class Cell which contains the majority of properties. Our Datagrid consists of template columns representing each of the cells in Row with relevant bindings. The problem is that when we do Datagrid Selecteditem we currently get a Model.Row returned whereas we would like to get the cell directly (Model.Row.TimeCell) for example if a cell in that column was clicked/selected. How can we achieve this?

You Need to cast DataGrid.SelectedItem To your custom class item ie

Row selRow = mainDataGrid.SelectedItem as Row;

Now you can able to access properties of Row ie

TimeCell timeCell = selRow.TimeCell;

Hope this will work

Thanks for letting us help you

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