开发者

wpf datagrid selection changed

开发者 https://www.devze.com 2023-01-06 03:44 出处:网络
I used to have the following code using a listview: private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e)

I used to have the following code using a listview:

private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (Exam exam in e.RemovedItems)
            {
                ViewModel.SelectedExams.Remove(exam);
            }
            foreach (Exam exam in e.AddedItems)
            {
                ViewModel.SelectedExams.Add(exam);
            }
        }

Now I changed to a datagrid but I'm getting an error message that says: Error 1 'System.EventArgs' does not contain a definition for 'RemovedItems' and no extension method 'RemovedItems' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

private void DataGridSelectionChanged(object sender, EventArgs e)
    {
        foreach (Exam exam in  e.RemovedItems)
        {
            ViewModel开发者_如何学JAVA.SelectedExams.Remove(exam);
        }
        foreach (Exam exam in e.AddedItems)
        {
            ViewModel.SelectedExams.Add(exam);
        }
    }

Can anyone out there help me by telling me how I can capture the selected rows in a datagrid in the same way as I used to capture them in a listview.


The SelectionChanged event of the DataGrid accepts SelectionChangedEventArgs as argument (the same as for the SelectionChanged event of the ListView), not the base EventArgs:

private void DataGridSelectionChanged(object sender, SelectionChangedEventArgs e)

Here e.RemovedItems and e.AddedItems are available.


SelectionChanged isn't even firing for me,

perhaps try SelectedCellsChanged? - More info at WPF Datagrid: SelectionChanged event isn't raised when SelectionUnit="Cell"

More about SelectionChanged on MSDN forums - http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e1dc5abc-2f94-4653-a0a8-ee9b6adcb165

Also getting the selected cell's value at WPF Toolkit DataGrid SelectionChanged Getting Cell Value

0

精彩评论

暂无评论...
验证码 换一张
取 消