【问题标题】:How to remove WPF ListViewItem selection in windows 7如何在 Windows 7 中删除 WPF ListViewItem 选择
【发布时间】:2012-01-08 00:29:31
【问题描述】:

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

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

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

【问题讨论】:

  • 当我在我的机器上将 focusable 设置为 false 时,无法选择 ListViewItems 并且鼠标悬停在 LvItem 上时,没有突出显示 ..(Win7)
  • 听起来像是主题不同。您的应用程序中有不同的主题吗?

标签: wpf windows-7 windows-xp


【解决方案1】:

考虑为 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>

【讨论】:

    【解决方案2】:

    只需使用ItemsControl 而不是 ListView。

    【讨论】:

      猜你喜欢
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 2017-10-15
      相关资源
      最近更新 更多