0

Hi I have a DataGrid with some rows, and now when I select one row I want to simultaneously fire up event with this selection, but I am having a problem.

This is my user control resources:

    <UserControl.Resources>
        <Style x:Key="CollapsedRow" TargetType="{x:Type DataGridRow}">

            <EventSetter Event="MouseLeftButtonDown" Handler="OnGroupChange" />

            <Setter Property="DetailsVisibility" Value="Collapsed" />

            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DodgerBlue" />
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="SteelBlue"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>

        </Style>
    </UserControl.Resources

And my datagrid xaml is simple:

<DataGrid RowStyle="{StaticResource CollapsedRow}" AutoGenerateColumns="false" CanUserAddRows="false" ItemsSource="{Binding Applications}" >

Well as you see I have a event setter for one mouse click that will fire up event and trigger that will change background colour of row if is selected. But it works like first you need to select row that one click and then fire event that is second click. is there a way to do it with one click?

1
  • 1
    did you try with PreviewMouseLeftButtonDown too? Commented Jan 3, 2019 at 9:53

1 Answer 1

1

The MouseLeftButtonDown event gets fired before the selection of the row gets changed. This means the property IsSelected of your row will be false. You could use the Selected event which gets fired after the selection was changed. Just add a new handler like

<EventSetter Event="Selected" Handler="OnSelected"/>

and

private void OnSelected(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Column selection change, IsSelected = " + ((DataGridRow)sender).IsSelected);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.