如何在WPF DataGrid中获取单元模板datacontext SelectedCellsChanged?

时间:2013-03-04 23:59:36

标签: wpf datagrid wpf-controls datagridcell

当选择单元格时,我必须更新单元格datacontext模型中的某些字段。

WPF datagrid包含事件 SelectedCellsChanged ,但我没有看到任何datacontext属性。是否有可能从当前事件中获取cell datacontext?

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。是否有可能从SelectedCellsChanged上的单元格模板获得控制权?

1 个答案:

答案 0 :(得分:0)

只需确保调试不是网格&#34;新行&#34;因为datacontext是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;
    }
}