Remove Highlight Effect from ListViewItem

前端 未结 7 1346
星月不相逢
星月不相逢 2020-12-01 04:11

In a ListView there are ListviewItems where they must not change appearance when the mouse is over them or they are selected.

I tried to a

相关标签:
7条回答
  • 2020-12-01 04:41

    I don't know if this is the only solution but I did it as follows (by setting the Template property of the ListViewItems):

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border
                             BorderBrush="Transparent"
                             BorderThickness="0"
                             Background="{TemplateBinding Background}">
                            <GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    

    EDIT:

    Or as Grant Winney suggests (I did not test that myself):

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    
    0 讨论(0)
  • 2020-12-01 04:46

    For me, worked well, like this:

    <ListView.ItemContainerStyle>
      <Style TargetType="ListViewItem">
        <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </ListView.ItemContainerStyle> 
    
    0 讨论(0)
  • 2020-12-01 04:49

    This answer is similar to that by Big Kev but for a GridView which needs the GridViewRowPresenter instead of the ContentPresenter.

    I liked Kev's answer best because it removed the default hover highlighting and still gives control over the style using the IsMouseOver Trigger which the other answers did not.

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" 
                                      BorderThickness="{TemplateBinding BorderThickness}" 
                                      Background="{TemplateBinding Background}">
                    <GridViewRowPresenter Content="{TemplateBinding Content}"
                                          Margin="{TemplateBinding Padding}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    
    0 讨论(0)
  • 2020-12-01 04:56

    This work:

    <Style TargetType="{x:Type ListViewItem}">  
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Grid Background="{TemplateBinding Background}">
                    <Border Name="Selection" Visibility="Collapsed" />
                    <!-- This is used when GridView is put inside the ListView -->
                    <GridViewRowPresenter Grid.RowSpan="2"
                                          Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    

    0 讨论(0)
  • 2020-12-01 04:58

    Did not the question but this worked for me .

    <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                                    <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" />
                                </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
    0 讨论(0)
  • 2020-12-01 05:05

    Try this one it worked for me.

                            <Style TargetType="{x:Type ListBoxItem}">
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                            <ContentPresenter HorizontalAlignment="Left" Margin="2,3,2,3" />
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
    
    0 讨论(0)
提交回复
热议问题