could the SelectionChanged event in WPF be handled only for user interaction?

穿精又带淫゛_ 提交于 2019-11-28 00:32:11

问题


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?


回答1:


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.

Sample code XAML:

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

Code behind:

bool isUserInteraction;

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

        isUserInteraction = false;
    }
}

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



回答2:


hi you can use this in xaml:

 <ComboBox x:Name="ComboBoxName" SelectionChanged="ComboBox_SelectionChanged">
                                        <ComboBox.Style>
                                            <Style TargetType="ComboBox">
                                                <Style.Triggers>                                                       
                                                    <Trigger Property="IsDropDownOpen" Value="True">
                                                        <Setter Property="IsEditable" Value="True"></Setter>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </ComboBox.Style>
                                    </ComboBox>

and in code behind:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!((ComboBox)sender).IsEditable) return;
        //Do Stuff;
    }



回答3:


Another method would be to handle the DropDownOpened and DropDownClosed events of the ComboBox. This is slightly better than the Rafal's accepted answer because it prevents the boolean flag getting stuck as true if the user clicked the ComboBox and then clicked somewhere else causing the ComboBox to close without a selection being made. What it doesn't solve, though, is if the ComboBox has keyboard focus and the user taps the up and down arrows to change the selection.

private void Event_ComboBox_DropDownOpened(object sender, EventArgs e)
{
    isUserInteraction = true;
}

private void Event_ComboBox_DropDownClosed(object sender, EventArgs e)
{
    isUserInteraction = false;
}

private void Event_ComboBox_SelectedChanged(object sender, SelectionChangedEventArgs e)
{
    if (isUserInteraction)
    {
        // Do work
    }
}


来源:https://stackoverflow.com/questions/14301271/could-the-selectionchanged-event-in-wpf-be-handled-only-for-user-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!