赞
踩
WPF中ListView样式设置ListViewStyle、ListViewItemStyle绑定双击事件、滚动条,双击改变选中条目颜色,但是滚动list后颜色消失
样式:
- <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}" >
- <Setter Property="Height" Value="30" />
- <Setter Property="Foreground" Value="{StaticResource TextBrush}" />
- <Setter Property="Background" Value="Transparent" />
- <Setter Property="HorizontalContentAlignment" Value="Stretch" />
- <Setter Property="BorderBrush" Value="Transparent" />
- <Setter Property="BorderThickness" Value="0" />
- <Setter Property="Margin" Value="0,0,0,1" />
- <Setter Property="Padding" Value="5,2,5,2" />
- <Setter Property="VerticalContentAlignment" Value="Center" />
- <Setter Property="Background">
- <Setter.Value>
- <Binding RelativeSource="{RelativeSource Self}" Converter="{StaticResource ItemBackgroundConverter}"/>
- </Setter.Value>
- </Setter>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type commonControl:ListViewItemEx}">
- <Border SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" x:Name="border">
- <Grid Margin="2,0,2,0">
- <Rectangle x:Name="Background" IsHitTestVisible="True" Opacity="1" Fill="{TemplateBinding Background}" RadiusX="1" RadiusY="1"/>
- <Rectangle x:Name="HoverRectangle" IsHitTestVisible="True" Opacity="0" Fill="#767779" RadiusX="1" RadiusY="1"/>
- <Rectangle x:Name="SelectedRectangle" IsHitTestVisible="True" Opacity="0" Fill="#767779" RadiusX="1" RadiusY="1"/>
- <GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,2,0,2" VerticalAlignment="Stretch" />
- </Grid>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsDoubleClick" Value="True">
- <Setter Property="Foreground" Value="#FF9740"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- <Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
- <Setter Property="SnapsToDevicePixels" Value="true" />
- <Setter Property="OverridesDefaultStyle" Value="true" />
- <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
- <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
- <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
- <Setter Property="VerticalContentAlignment" Value="Center" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type commonControl:ListViewEx}">
- <Border Name="Border" BorderThickness="1" Background="{x:Null}">
- <Border.BorderBrush>
- <SolidColorBrush Color="{StaticResource BorderMediumColor}" />
- </Border.BorderBrush>
- <ScrollViewer Style="{StaticResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
- <ItemsPresenter />
- </ScrollViewer>
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
解决方案:
在 WPF 中,如果你通过样式设置双击 ListView 后改变选中条目颜色,但是滚动 ListView 后颜色消失,可能是由于虚拟化(Virtualization)引起的。
默认情况下,WPF 的 ListView 和一些其他控件启用了虚拟化机制,这意味着只有当前可见的部分才会被渲染,而不是所有项。这可以提高性能,特别是在处理大量数据时。
当使用虚拟化时,当你滚动 ListView 时,已经渲染的项将被重用,而不是重新创建,因此,你的样式设置可能不会被应用于重用的项。
为了解决这个问题,你可以通过设置 ListView 的 VirtualizingStackPanel.IsVirtualizing 属性为 False,禁用虚拟化机制,以确保所有项都被渲染,并且你的样式设置将被应用于所有项。
例如,你可以在 ListView 的 XAML 中添加以下属性:
<ListView VirtualizingStackPanel.IsVirtualizing="False">
<!-- your list view items and other properties -->
</ListView>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。