如何在Windows 7中删除WPF ListViewItem选择

时间:2011-11-28 14:55:56

标签: wpf windows-7 windows-xp

我想关闭ListViewItems上的选择,我不希望当鼠标悬停时突出显示该行。 我在windows xp系统上安装了应用程序,并使用以下代码禁用行选择:

<ListView.ItemContainerStyle>
     <Style TargetType="{x:Type ListViewItem}">
         <!--Disables selecting the row-->
         <Setter Property="Focusable" Value="false"/>
     </Style>
</ListView.ItemContainerStyle>

这样可行,但相同的代码在Windows 7中不起作用。

2 个答案:

答案 0 :(得分:0)

考虑为ListViewItem提取默认样式并将所有颜色设置为透明。这个解决方案的好处是,你的ListView在Win7和XP上看起来都一样。此解决方案不会阻止用户选择项目,只会使选择不可见。可能是这种风格可以简化一些。这是我完整的XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2" SnapsToDevicePixels="true">
                            <Border x:Name="InnerBorder" BorderThickness="1" CornerRadius="1">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition MaxHeight="11"/>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <Rectangle x:Name="UpperHighlight" Fill="#75FFFFFF" Visibility="Collapsed"/>
                                    <GridViewRowPresenter Grid.RowSpan="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Grid>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="BorderBrush" Value="Transparent"/>
                                <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="BorderBrush" Value="Transparent"/>
                                <Setter Property="BorderBrush" TargetName="InnerBorder" Value="Transparent"/>
                                <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
                                <Setter Property="Fill" TargetName="UpperHighlight" Value="#40FFFFFF"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="Selector.IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="BorderBrush" Value="Transparent"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="BorderBrush" Value="Transparent"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn/>
                    <GridViewColumn/>
                    <GridViewColumn/>
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
            <ListViewItem Content="ListViewItem" />
            <ListViewItem Content="ListViewItem" />
            <ListViewItem Content="ListViewItem" IsSelected="True"/>
            <ListViewItem Content="ListViewItem" />
            <ListViewItem Content="ListViewItem" />
            <ListViewItem Content="ListViewItem" />
            <ListViewItem Content="ListViewItem" />
        </ListView>
        <Button Height="30"></Button>
    </StackPanel>

</Window>

答案 1 :(得分:0)

只需使用ItemsControl而不是ListView。

相关问题