Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 986
binyame tiruneh Avatar asked Jan 13 '13 06:01

binyame tiruneh


2 Answers

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;
}
like image 109
Rafal Avatar answered Oct 27 '22 10:10

Rafal


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;
    }
like image 20
luka Avatar answered Oct 27 '22 11:10

luka