.net 从 ListViewItem 中删除高亮效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16477552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:14:33  来源:igfitidea点击:

Remove Highlight Effect from ListViewItem

.netwpfxamlstylesitemcontainerstyle

提问by mskydt86

In a ListViewthere are ListviewItemswhere they must not change appearance when the mouse is over them or they are selected.

ListView某些ListviewItems地方,当鼠标悬停在它们上方或它们被选中时,它们不得改变外观。

I tried to accomplish that with this style and did somewhatsucceed:

我试图用这种风格来实现这一点,并取得了一些成功:

<Style x:Key="ItemContainerStyle1" TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Focusable" Value="False" />
        </Trigger>
    </Style.Triggers>
</Style>

But the it raised a new issue. When the background is set to "Transparent" I am now able to see this hover/glossy effect that is shown at the picture below, when the mouse is over a list view item.

但它提出了一个新问题。当背景设置为“透明”时,当鼠标悬停在列表视图项上时,我现在可以看到下图所示的悬停/光泽效果。

enter image description here

在此处输入图片说明

I have tried to solve the problem with this attempt, but with no luck.

我试图通过这次尝试解决问题,但没有运气。

<Style TargetType="{x:Type ListViewItem}">
    <Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000"/>
    </Style.Resources>
</Style>

Anyone have an idea how to remove this hover effect?

任何人都知道如何消除这种悬停效果?

回答by Matmarbon

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

我不知道这是否是唯一的解决方案,但我是这样做的(通过设置s的Template属性ListViewItem):

<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>

回答by Rodrigo Quintas

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>

回答by Sim?o Ferreira

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> 

回答by big kev

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>

回答by Lingua94

I had the same problem, but none of the answers helped me. Searching on the web i found this solution and it worked:

我遇到了同样的问题,但没有一个答案对我有帮助。在网上搜索我找到了这个解决方案并且它有效:

        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <GridViewRowPresenter />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>

回答by Joe Kolodz

This answer is similar to that by Big Kev but for a GridViewwhich needs the GridViewRowPresenterinstead of the ContentPresenter.

这个答案与 Big Kev 的答案相似,但对于 aGridView需要GridViewRowPresenter而不是ContentPresenter.

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

我最喜欢 Kev 的答案,因为它删除了默认的悬停突出显示,并且仍然使用IsMouseOver其他答案没有的Trigger 来控制样式。

<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>

回答by V-Wanderer

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>