禁用ListView上的选择/鼠标悬停效果

本文关键字:鼠标 悬停 选择 ListView 禁用 | 更新日期: 2023-09-27 18:28:49

我有一个ListView,我想在其中禁用蓝色MouseOver/Selection效果。我在网上找到了很多解决方案,但没有一个对我有效。可能是因为我使用的是ItemTemplateSelector吗?这是我的ListView:

<ListView x:Name="lvNachrichten" Margin="10,0,0,5" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Focusable" Value="false"/>
                    <Style.Triggers>
                        <Trigger Property="Control.IsMouseOver" Value="True">
                            <Setter Property="Control.Background" Value="Transparent" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.Background>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}"/>                    
            </ListView.Background>                
            <ListView.ItemTemplateSelector>
                <local:ListViewItemTemplateSelector>                        
                    <local:ListViewItemTemplateSelector.SenderTemplate>
                        <DataTemplate>
                            <Border Background="PeachPuff" CornerRadius="4" Padding="5,8" HorizontalAlignment="Left" Margin="0,0,0,3" Focusable="False">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" MaxWidth="{Binding ElementName=lvNachrichten, Path=ActualWidth, Converter={StaticResource ArithmeticConv},
                                                          ConverterParameter=Int32.Parse(values[0])*0.75}" />
                                        <ColumnDefinition Width="5" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding MailText}" FontSize="16" TextWrapping="Wrap" />
                                    <!--<CheckBox Grid.Column="2" IsChecked="{Binding gesendet}" IsHitTestVisible="False" VerticalAlignment="Bottom" Template="{StaticResource CheckedImage}" />
                                    <CheckBox Grid.Column="3" IsChecked="{Binding gelesen}" IsHitTestVisible="False" VerticalAlignment="Bottom" Template="{StaticResource CheckedImage}" />-->
                                    <Image Width="28" Height="15" Grid.Column="2" VerticalAlignment="Bottom">
                                        <Image.Source>
                                            <MultiBinding Converter="{StaticResource MessageCheckConv}">
                                                <MultiBinding.Bindings>
                                                    <Binding Path="gesendet" />
                                                    <Binding Path="gelesen" />
                                                </MultiBinding.Bindings>
                                            </MultiBinding>
                                        </Image.Source>
                                    </Image>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </local:ListViewItemTemplateSelector.SenderTemplate>
                    <local:ListViewItemTemplateSelector.EmpfaengerTemplate>
                        <DataTemplate>
                            <Border Background="CornflowerBlue" CornerRadius="4" Padding="5,8" HorizontalAlignment="Right" Margin="0,0,0,3">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="5" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding MailText}" HorizontalAlignment="Right" FontSize="16" TextWrapping="Wrap" />
                                    <!--<CheckBox Grid.Column="2" IsChecked="{Binding Gesendet}" IsHitTestVisible="False" HorizontalAlignment="Right"/>
                                <CheckBox Grid.Column="3" IsChecked="{Binding gelesen}" IsHitTestVisible="False" HorizontalAlignment="Right"/>-->
                                    <Image Width="28" Height="15" Grid.Column="2" VerticalAlignment="Bottom">
                                        <Image.Source>
                                            <MultiBinding Converter="{StaticResource MessageCheckConv}">
                                                <MultiBinding.Bindings>
                                                    <Binding Path="gesendet" />
                                                    <Binding Path="gelesen" />
                                                </MultiBinding.Bindings>
                                            </MultiBinding>
                                        </Image.Source>
                                    </Image>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </local:ListViewItemTemplateSelector.EmpfaengerTemplate>
                </local:ListViewItemTemplateSelector>
            </ListView.ItemTemplateSelector>
        </ListView>

禁用ListView上的选择/鼠标悬停效果

您应该重新为ListViewItem的控制模板设置模板,然后您就可以管理它的所有效果。当您将数据模板提供给ListViewItem时,您只需定义数据的外观,从另一方面更改控件模板可以定义控件的视觉行为(例如控件将具有的效果)。您可以尝试使用下一个代码作为起点:1.Xaml编码:

 <Window x:Class="SoListBoxStyleHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <x:Array x:Key="StringArray" Type="system:String">
        <system:String>Examle Two</system:String>
        <system:String>Examle Three</system:String>
        <system:String>Examle Five</system:String>
        <system:String>Examle Seven</system:String>
    </x:Array>
</Window.Resources>
<Grid>
    <ListBox ItemsSource="{StaticResource StringArray}" Background="#00FFFFFF">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="BorderThickness" Value="1"></Setter>
                 <Setter Property="BorderBrush" Value="DimGray"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid x:Name="Grid">
                                <Border x:Name="SimpleBackground" Visibility="Visible" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Border>
                                <Border x:Name="OnSelectedBackground" Visibility="Collapsed" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="Tomato" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Border>
                                <ContentPresenter x:Name="ContentPresenter"></ContentPresenter>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="OnSelectedBackground" Property="Visibility" Value="Visible"></Setter>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter TargetName="OnSelectedBackground" Property="Visibility" Value="Collapsed"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

如果你有任何问题,我很乐意帮忙。问候,