Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4208

WPF datagrid selected row and binding event to it

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?

Upvotes: 0

Views: 3146

Answers (1)

Fruchtzwerg
Fruchtzwerg

Reputation: 11399

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);
}

Upvotes: 1

Related Questions