ListBox滚动不起作用

时间:2012-03-06 11:06:03

标签: wpf listbox

ListBoxEx派生自ListBox。

其默认样式为

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ControlsLibrary">
    <Style TargetType="{x:Type local:ListBoxEx}">
        <Setter Property="SelectAllToggleStyle">
            <Setter.Value>
                <Style TargetType="ToggleButton">
                    <Setter Property="Content" Value="Select All"/>
                    <Setter Property="Focusable" Value="True"/>
                    <Setter Property="Margin" Value="2,0,0,0"/>
                    <Setter Property="IsThreeState" Value="False"/>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ListBoxEx}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="true"
                            Padding="{TemplateBinding Padding}"
                            VerticalAlignment="{TemplateBinding VerticalAlignment}"
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                            Height="{TemplateBinding Height}"
                            Width="{TemplateBinding Width}">
                        <DockPanel VerticalAlignment="Top" HorizontalAlignment="Stretch" LastChildFill="True">
                            <CheckBox Name="PART_SelectAllToggle" DockPanel.Dock="Top"/>
                            <Separator Margin="4" Visibility="{Binding Path=Visibility, ElementName=PART_SelectAllToggle}" DockPanel.Dock="Top"/>
                            <ItemsPresenter DockPanel.Dock="Top"/>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style>
                    <Setter Property="ListBoxItem.Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border
                                SnapsToDevicePixels="true" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                Height="{TemplateBinding Height}"
                                Width="{TemplateBinding Width}">

                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="{x:Static SystemColors.HighlightBrush}"/>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Background" Value="{x:Static SystemColors.HotTrackBrush}"/>
                                        <Setter Property="Foreground" Value="White"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Control.VerticalAlignment" Value="Center"/>
                    <Setter Property="Control.FocusVisualStyle">
                        <Setter.Value>
                            <Style TargetType="Control">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Rectangle Margin="0" StrokeThickness="2"  Stroke="Black" SnapsToDevicePixels="true"/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Padding" Value="4"/>
    </Style>
</ResourceDictionary>

没有堆叠面板。

测试窗口的xaml是:

<Window x:Class="WpfTesting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cl="clr-namespace:ControlsLibrary;assembly=ControlsLibrary" xmlns:System="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="300">
    <DockPanel Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <cl:ListBoxEx  BorderThickness="2" BorderBrush="Black" ShowSelectAllToggle="True" Height="100" DockPanel.Dock="Top">
            <cl:ListBoxEx.Items>
                <System:String>QWERTY1</System:String>
                <System:String>QWERTY2</System:String>
                <System:String>QWERTY3</System:String>
                <System:String>QWERTY4</System:String>
                <System:String>QWERTY5</System:String>
                <System:String>QWERTY6</System:String>
                <System:String>QWERTY7</System:String>
                <System:String>QWERTY8</System:String>
            </cl:ListBoxEx.Items>
        </cl:ListBoxEx>
    </DockPanel>
</Window>

我无法理解为什么滚动不起作用。 =( 可能我错过了布局的东西......但是什么?

1 个答案:

答案 0 :(得分:1)

您的ControlTemplate不包含ScrollViewer。 (否ScrollViewer - &gt;无滚动)