Domanda

I would like to update some fields in a cell datacontext model during the cell selection.

WPF datagrid contains event SelectedCellsChanged but I cannot find any datacontext property. Is it possible to get cell datacontext from current event?

private void OnSelectedCellChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            var firstCell=e.AddedCells.FirstOrDefault();
            //How can i get cell datacontext?
        
        }

<DataGrid SelectionUnit="CellOrRowHeader" 
    x:Name="grid"
        SelectedCellsChanged="OnSelectedCellChanged"
        ItemsSource="{Binding Items}" ...

P.s. is it possible to get control from cell template on SelectedCellsChanged? Thank you in advance!

È stato utile?

Soluzione

Just make sure that you debugging not grid "new line" because there datacontext is null! :)

foreach (var item in e.AddedCells)
{
    var column = item.Column as FocusableDataGridTemplateColumn;
    if (column != null)
    {
        var presenter=item.Column.GetCellContent(item.Item);
        presenter.DataContext;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top