2016-11-16 66 views
0

需要使用擴展器來構建圖像瀏覽器,但是我有一些滾動問題。我有ItemsControl裏面的ListBox,滾動不起作用時,在ListBox上的鼠標。這裏是xaml:WPF滾動不能在ListBox裏面ItemsControl

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Grid.Row="1" Background="{DynamicResource LightGrayBackgroundBrush}" > 
    <ItemsControl x:Name="itmsControl" DataContext="{Binding ElementName=_self}" ItemsSource="{Binding ImagesSource}" Margin="15" > 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid x:Name="grdIn"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" MinHeight="25"/> 
         <RowDefinition x:Name="grd1"/> 
        </Grid.RowDefinitions> 

        <Expander Grid.Row="1" IsExpanded="True" BorderThickness="0" Background="White"> 
         <Expander.Header> 
          <Border Background="White" BorderBrush="White" Height="40"> 
           <TextBlock Text="{Binding Date}" Background="White" FontSize="14" Foreground="Gray" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0,0,0"/> 
          </Border> 
         </Expander.Header> 

         <ListBox ItemsSource="{Binding ImageList}" ItemContainerStyle="{DynamicResource ImageListBoxItemStyle}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Extended" Background="Transparent" SelectionChanged="ListBox_SelectionChanged" PreviewKeyDown="OnKeyDownHandler" MouseDown="ListBox_MouseDown" ScrollViewer.CanContentScroll="False"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <Image Stretch="UniformToFill" Width="{Binding Width}" Height="{Binding Height}" Source="{Binding Source}" Margin="3" MouseDown="Image_MouseDown"/> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
          <ListBox.ItemsPanel> 
           <ItemsPanelTemplate> 
            <WrapPanel IsItemsHost="True" Orientation="Horizontal" /> 
           </ItemsPanelTemplate> 
          </ListBox.ItemsPanel> 
         </ListBox> 
        </Expander> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</ScrollViewer> 

回答

0

當您選擇列表框圖像,焦點失去了滾動查看器。 因此,您可以將焦點設置爲在MouseWheel/PreviewMouseWheel事件上滾動查看器,或者您可以像下面那樣手動滾動。

myScroll.ScrollToVerticalOffset(myScroll.VerticalOffset + 10); 
+0

我添加PreviewMouseWheel到列表框: 私人無效ListBox_PreviewMouseWheel(對象發件人,MouseWheelEventArgs E) { mainScroll.ScrollToVerticalOffset(mainScroll.VerticalOffset - e.Delta); } 這是作品,謝謝Maulik :) –

0

想滾動ScrollViewer嗎?那麼你爲什麼要在ItemsControl中使用ListBox?我看你阻止滾動列表框

ScrollViewer.CanContentScroll="False" 

但是ListBox在他的模板中有ScrollViewer。我認爲這個ScrollViewer處理「鼠標whell」事件,它不會到達根ScrollViewer。 我認爲你可以解決你的問題,如果你只是將ListBox替換爲ItemsControl。

0

變化XAML與此:

<ListBox.ItemsPanel> 
           <ItemsPanelTemplate> 
<ScrollViewer> 
            <WrapPanel IsItemsHost="True" Orientation="Horizontal" /> 
</ScrollViewer> 
           </ItemsPanelTemplate> 
          </ListBox.ItemsPanel>