本文介绍了WPF中的SelectionChanged事件是否只能用于用户交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想为用户交互/选择处理WPF DataGrid元素中的SelectionChanged事件,如果是由于绑定或其他设置值而跳过.知道如何确定用户交互是否更改了选择吗?还是其他任何可以完成类似任务的事件?

I would like to handled SelectionChanged event in WPF DataGrid element for user interaction/selection only and skip if it's due to binding or other set values. Any idea how I will determine if the Selection is changed by user interaction? Or any alternate event that would do similar task?

推荐答案

也许尝试将SelectionChanged事件与PreviewMouseDown事件结合在一起.当用户单击一行时,您设置了一些属性,然后在SelectionChanged事件处理程序中检查than属性是否已更改.

Maybe try combine SelectionChanged event with PreviewMouseDown event. When user click a row you set some property and in SelectionChanged event handler check if than property was changed.

示例代码XAML:

<DataGrid SelectionChanged="OnSelectionChanged" PreviewMouseDown="OnPreviewMouseDown">
        <!--some code-->          
</DataGrid>

后面的代码:

bool isUserInteraction;

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (isUserInteraction)
    {
        //some code

        isUserInteraction = false;
    }
}

private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    isUserInteraction = true;
}

这篇关于WPF中的SelectionChanged事件是否只能用于用户交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 03:59