【问题标题】:WPF Datagrid: SelectionChanged event isn't raised when SelectionUnit="Cell"WPF Datagrid:SelectionUnit="Cell" 时未引发 SelectionChanged 事件
【发布时间】:2011-01-17 14:34:50
【问题描述】:

我正在使用 WPF 工具包数据网格。我已将其设置为 SelectionUnit="Cell" 和 SelectionMode="Extended"。

永远不会引发 SelectionChanged 事件!

当 SelectionUnit 设置为 FullRow 时,它可以正常工作。

我错过了什么吗?

顺便说一句,我需要它的原因是因为我正在尝试创建一个附加属性来帮助我将 SelectedCells 绑定到我的 ViewModel。

【问题讨论】:

    标签: wpf datagrid wpfdatagrid


    【解决方案1】:

    使用DataGrid.SelectedCellsChanged 应该provide you 满足您的需要。

    private void DG1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        //Get the newly selected cells
        IList<DataGridCellInfo> selectedcells = e.AddedCells;
    
        //Get the value of each newly selected cell
        foreach (DataGridCellInfo di in selectedcells)
        {
            //Cast the DataGridCellInfo.Item to the source object type
            //In this case the ItemsSource is a DataTable and individual items are DataRows
            DataRowView dvr = (DataRowView)di.Item;
    
            //Clear values for all newly selected cells
            AdventureWorksLT2008DataSet.CustomerRow cr = (AdventureWorksLT2008DataSet.CustomerRow)dvr.Row;
            cr.BeginEdit();
            cr.SetField(di.Column.DisplayIndex, "");
            cr.EndEdit();
    
        }
    }
    

    【讨论】:

    • 亚伦说得对。 SelectionChanged 确实有效并提供了所需的信息。
    猜你喜欢
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多