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

时间:2016-01-04 10:52:39

标签: c# wpf listview

我有一个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>

1 个答案:

答案 0 :(得分:0)

您应该重新模板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>

如果你有任何问题,我会很乐意提供帮助。 的问候,