修改了ListBox滚动条的样式,修改后虚拟化就失效了.
本来想先看下ListBox滚动条的默认样式;
在这里先看看有大侠知道怎么解决不?

解决方案 »

  1.   

    样式代码,修改了滚动条的样式<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush x:Key="StandardBorderBrush" Color="#888" />
        <SolidColorBrush x:Key="StandardBackgroundBrush" Color="#FFF" />
        <SolidColorBrush x:Key="HoverBorderBrush" Color="#DDD" />
        <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="Gray" />
        <SolidColorBrush x:Key="SelectedForegroundBrush" Color="White" />
        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
        <SolidColorBrush x:Key="NormalBrush" Color="#888" />
        <SolidColorBrush x:Key="NormalBorderBrush" Color="#888" />
        <SolidColorBrush x:Key="HorizontalNormalBrush" Color="#888" />
        <SolidColorBrush x:Key="HorizontalNormalBorderBrush" Color="#888" />    <Style x:Key="stBackBorder" TargetType="Border">
            <Setter Property="BorderBrush" Value="#909090"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Background" Value="#909090"/>
            <Setter Property="CornerRadius" Value="1"/>
        </Style>
        <LinearGradientBrush x:Key="ListBoxBackgroundBrush"
                StartPoint="0,0" EndPoint="1,0.001">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="White" Offset="0.0" />
                    <GradientStop Color="White" Offset="0.6" />
                    <GradientStop Color="#DDDDDD" Offset="1.2"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="StandardBrush"
                StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="#CCC" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
        <LinearGradientBrush x:Key="PressedBrush"
                StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#BBB" Offset="0.0"/>
                    <GradientStop Color="#EEE" Offset="0.1"/>
                    <GradientStop Color="#EEE" Offset="0.9"/>
                    <GradientStop Color="#FFF" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>    <Style x:Key="ScrollBarLineButton" TargetType="{x:Type RepeatButton}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Border
              Name="Border"
              Margin="1"
              CornerRadius="2"
              Background="{StaticResource NormalBrush}"
              BorderBrush="{StaticResource NormalBorderBrush}"
              BorderThickness="1">
                            <Path
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Fill="{StaticResource GlyphBrush}"
                Data="{Binding Path=Content,
                    RelativeSource={RelativeSource TemplatedParent}}" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="Border" Property="Background"
                                    Value="{StaticResource PressedBrush}" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground"
                                    Value="{StaticResource DisabledForegroundBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>    <Style x:Key="ScrollBarPageButton" TargetType="{x:Type RepeatButton}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>        <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">                    <Border Background="#caa948" />                </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>    <Style x:Key="ScrollBottomBarPageButton" TargetType="{x:Type RepeatButton}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Border Background="#909090" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>    <!--设置中间滚动条-->
        <Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="Width" Value="11"/>
            <Setter Property="Height" Value="Auto"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition  Height="*" MaxHeight="11"/>
                                    <RowDefinition  Height="*"/>
                                    <RowDefinition  Height="*" MaxHeight="11"/>
                                </Grid.RowDefinitions>
                                <Border Grid.Row="0">
                                    <!--头部图片-->
                                    <Border.Background>
                                        <ImageBrush ImageSource="/WPF.TControls;component/Images/rolltop.png"/>
                                    </Border.Background>
                                </Border>
                                <Border Grid.Row="1">
                                    <!--中间图片,设置成自动拉长-->
                                    <Border.Background>
                                        <ImageBrush ImageSource="/WPF.TControls;component/Images/rollback.png"  TileMode="FlipY"  />
                                    </Border.Background>
                                </Border>
                                <Border Grid.Row="2">
                                    <!--底部图片-->
                                    <Border.Background>
                                        <ImageBrush ImageSource="/WPF.TControls;component/Images/rollbottom.png"/>
                                    </Border.Background>
                                </Border>
                            </Grid>
                        </Border>
                        <!--"{TemplateBinding Background}"-->
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>    <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="18"/>
                    <RowDefinition />
                    <RowDefinition Height="18"/>
                </Grid.RowDefinitions>
                <!--上按钮-->
                <RepeatButton Grid.Row="0" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineUpCommand" Content="M 0 4 L 4 0 L 8 4 Z"/>
                <Track Name="PART_Track" Grid.Row="1" IsDirectionReversed="true" >
                    <Track.DecreaseRepeatButton>
                        <RepeatButton Width="5" Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageUpCommand" />
                    </Track.DecreaseRepeatButton>
                    <Track.Thumb>
                        <Thumb Style="{StaticResource ScrollBarThumb}"  BorderBrush="{StaticResource HorizontalNormalBorderBrush}">
                        </Thumb>
                    </Track.Thumb>
                    <Track.IncreaseRepeatButton>
                        <RepeatButton Width="5" Style="{StaticResource ScrollBottomBarPageButton}" Command="ScrollBar.PageDownCommand" />
                    </Track.IncreaseRepeatButton>
                </Track>
                <!--下按钮-->
                <RepeatButton Grid.Row="3" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineDownCommand" Content="M 0 0 L 4 4 L 8 0 Z"/>
            </Grid>
        </ControlTemplate>    
      

  2.   

    <Style TargetType="{x:Type ScrollViewer}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>                        <ScrollContentPresenter/>                        <ScrollBar Name="PART_VerticalScrollBar"
                                           Grid.Column="1"
                                           Value="{TemplateBinding VerticalOffset}"
                                           Maximum="{TemplateBinding ScrollableHeight}"
                                           Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                           Template="{StaticResource VerticalScrollBar}"
                                           ViewportSize="{TemplateBinding ViewportHeight}"
                                        />
                            <!--<ScrollBar Name="PART_HorizontalScrollBar"
                                           Orientation="Horizontal"
                                           Grid.Row="1"
                                           Value="{TemplateBinding HorizontalOffset}"
                                           Maximum="{TemplateBinding ScrollableWidth}"
                                           ViewportSize="{TemplateBinding ViewportWidth}"
                                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                           />
                            -->                    </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>    <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Style.Triggers>
                <!--<Trigger Property="Orientation" Value="Vertical">
                    <Setter Property="Width" Value="10"/>
                    <Setter Property="Height" Value="Auto" />
                    <Setter Property="Template"
                            Value="{StaticResource VerticalScrollBar}" />
                </Trigger>-->
            </Style.Triggers>
        </Style></ResourceDictionary>
      

  3.   

    listbox开启虚拟化 VirtualizingStackPanel.IsVirtualizing="True"
    <ListBox VirtualizingStackPanel.IsVirtualizing="True" x:Name="listBox1" Background="Transparent" 
                     ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
            </ListBox>去掉样式就可以正常虚拟化,我想因该使样式里有什么地方要编辑下,有问遇到这样的问题不?
      

  4.   

    一段段比较默认的样式,找到了,要改下ScrollViewer...
    <ScrollContentPresenter Grid.Column="0" Grid.Row="0" Margin="{TemplateBinding Control.Padding}" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}" />
      

  5.   

    wpf 修改了样式 其实不是虚拟化消失的主要原因,问题有点久了,回答下 为后来者 提点思路,第一设置ScrollViewer.CanContentScroll="True"  VirtualizingStackPanel.IsVirtualizing="True" 第二重写虚拟化面板