2017-02-24 119 views
0

ListBoxItemsPanelTemplate使用WrapPanelVerticalOrientationVerticalScrollbarVisibility設置爲Disabled,我不能滾動我的內容在水平方向上的鼠標滾輪。WPF列表框wrappanel滾動verticle方向

我想使我的列表框看起來像它可以在水平方向上滾動,但項目應該相對於窗口高度從上到下方向出現。

項目應該出現在這種方式如下

1 4 7 10 
2 5 8 11 ... 
3 6 9 12 

主要的問題是我無法用鼠標滾動。使用滾動條效果很好,使用鍵盤選擇效果很好。

這裏就是我所做的

<ListBox ItemContainerStyle="{StaticResource ResourceKey=ContainerStyle}" Background="{StaticResource StaticBackground}" ItemsSource="{Binding ListSource}" ScrollViewer.VerticalScrollBarVisibility="Disabled"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel Orientation="Vertical" ScrollViewer.VerticalScrollBarVisibility="Disabled"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox> 

Flow of data from top to bottom with horizontal orientation

,這是我ItemContainerStyle

<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"> 
    <Setter Property="ContentTemplate" Value="{StaticResource DT_TestTemplate}" /> 
    <Setter Property="SnapsToDevicePixels" Value="True" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Border x:Name="Border" 
         Margin="5,5,5,5" 
         SnapsToDevicePixels="true"> 
        <Border.Background> 
         <SolidColorBrush Color="Transparent" /> 
        </Border.Background> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="SelectionStates"> 
          <VisualState x:Name="Unselected" > 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
              Storyboard.TargetProperty="(Panel.Background). 
       (SolidColorBrush.Color)"> 
             <EasingColorKeyFrame KeyTime="0" 
            Value="White" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Selected"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
              Storyboard.TargetProperty="(Panel.Background). 
       (SolidColorBrush.Color)"> 
             <EasingColorKeyFrame KeyTime="0" 
            Value="#FFF34235" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="SelectedUnfocused"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" 
              Storyboard.TargetProperty="(Panel.Background). 
       (SolidColorBrush.Color)"> 
             <EasingColorKeyFrame KeyTime="0" 
            Value="#FFF34235" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentPresenter /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True" > 
      <!--<Setter Property="ContentTemplate" Value="{StaticResource DT_TestTemplateSelectedItem}" />--> 
      <Setter Property="BorderBrush" Value="Transparent" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

我該怎麼辦,使其與鼠標滾輪滾動?

+0

澄清你的問題,因爲它不清楚。 – Aybe

回答

0

我用ScrollViewer作爲我的列表框的父容器,並在代碼後面處理了它的PreviewMouseWheel事件。

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" PreviewMouseWheel="ScrollViewer_PreviewMouseWheel"> 
    <ListBox ItemContainerStyle="{StaticResource ResourceKey=ContainerStyle}" Background="{StaticResource StaticBackground}" ItemsSource="{Binding ListSource}" ScrollViewer.VerticalScrollBarVisibility="Disabled"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Vertical" Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, Mode=FindAncestor}}" ScrollViewer.VerticalScrollBarVisibility="Disabled"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 
</ScrollViewer> 

這是我背後的代碼。

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    ScrollViewer viewer = sender as ScrollViewer; 
    viewer.ScrollToHorizontalOffset(viewer.HorizontalOffset - e.Delta); 
} 

找到這個post有幫助。