Search code examples
c#wpflistviewitem

Remove Listview mouse over


I have a simple list view.

<ListView  x:Name="DatabasesLstVw" ItemsSource="{Binding Path=Issues}"
                    ItemContainerStyle="{StaticResource removeMouseOverStyle}" 
                   AlternationCount="2" Grid.Row="1" Margin="20,10,20,0" 
                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                   FontSize="12" FontWeight="Normal" 
                   BorderThickness="0" Background="Transparent"
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
            <ListView.Resources>
                <Style TargetType="GridViewColumnHeader">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="Message">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Name}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

I have created a style trying to remove the default mouse over and select styling.

 <Style x:Key="removeMouseOverStyle" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>

                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Transparent"></Setter>
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Margin" Value="0,0,0,0"/>
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="FontSize" Value="12"/>
                </Trigger>
                <Trigger Property="ItemsControl.IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Margin" Value="0"/>
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="FontSize" Value="12"/>     
                    <Setter Property="FontWeight" Value="Normal"/>
                </Trigger>
            </Style.Triggers>
        </Style>

I cant post what its doing without trying to create a gif picture of it. Basically its hopping a little with the mouse over. First I thought it was setting margin , then tried padding and font size.

What exactly is this default mouse over doing and how do I remove it?


Solution

  • There could be many ways how they could have implemented selection\mouse over. For example often it is done by showing separate Border for every state. For control such simple as ListViewItem it's better to override it's ControlTemplate and do what you need there. You can use default control template, just remove triggers:

    <Style x:Key="removeMouseOverStyle"
               TargetType="ListViewItem">
            <Setter Property="SnapsToDevicePixels"
                    Value="true" />
            <Setter Property="OverridesDefaultStyle"
                    Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="Border"
                                Padding="2"
                                SnapsToDevicePixels="true"
                                Background="Transparent">
                            <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    </Style>
    

    With this style items won't change their appearence on selection or mouse over. Also, you can use base ItemsControl control - it does not have selection\mouse over behavior by default.