Search code examples
wpfxamlgridviewismouseover

How do i style the rows in a GridView?


I have a WPF project where i have a GridView nested inside a ListView and i want to change the style for the rows.

I want to change the color for when the cursor is above.

How do i do that?

Here is my XAML

<ListView x:Name="list_User_Events" Background="{StaticResource BorderDarkGreen}" Foreground="White" BorderThickness="0" BorderBrush="{x:Null}" SelectionMode="Single">
<ListView.View>
    <GridView>
        <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
        <GridViewColumn Width="50" Header="Interval" DisplayMemberBinding="{Binding Interval}"></GridViewColumn>
        <GridViewColumn Width="160" Header="Type" DisplayMemberBinding="{Binding EventType}"></GridViewColumn>
        <GridViewColumn Header="Command" DisplayMemberBinding="{Binding Command}"></GridViewColumn>
        <GridViewColumn Header="Action">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button x:Name="btn_list_user_Event_Delete" Click="btn_list_user_Event_Delete_Click">Delete</Button>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>
</ListView>

Solution

  • You'll need to set the ItemContainerStyle on the ListView to a Style with a TargetType of ListViewItem:

    <ListView 
        x:Name="list_User_Events" 
        Background="{StaticResource BorderDarkGreen}" 
        Foreground="White" 
        BorderThickness="0" 
        BorderBrush="{x:Null}" 
        SelectionMode="Single"
        >
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <!-- Setters, Triggers, etc. -->
            </Style>
        </ListView.ItemContainerStyle>
    
        <ListView.View>
            <!-- etc. etc. snip snip -->
        </ListView.View>
    </ListView>
    

    If you want to reuse the style for multiple similar listviews, you would define it in Window.Resources or UserControl.Resources with an x:Key attribute and use it as a static resource:

    <ListView
        ItemContainerStyle="{StaticResource ListViewGridViewItemStyle}"