2012-03-06 114 views
1

ListBoxEx從ListBox派生。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

ControlTemplate不包含ScrollViewer(無ScrollViewer - ?。?>無滾動)

+0

爲什麼要它,如果VirtualizingStackPanel支持滾動 – 2012-03-06 11:18:38

+0

@voroninp:誰說的?VirtualizingStackPanel只是一個面板,它只展示了一些東西,僅此而已。 – 2012-03-06 11:19:46

+0

Hummm ...可能是我理解錯誤的文檔 – 2012-03-06 11:23:22