WPF滚动不适用于ItemsControl内的ListBox [英] WPF scroll do not working on ListBox inside ItemsControl
本文介绍了WPF滚动不适用于ItemsControl内的ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要使用扩展器来构建图像资源管理器,但是滚动存在一些问题.我有带ListBox的ItemsControl,将鼠标放在ListBox上时滚动不起作用.这是xaml:
Need to build image explorer with expanders, but I have some problem with scrolling. I have ItemsControl with ListBox inside, scroll do not work when mouse on ListBox. Here is xaml:
<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>
推荐答案
当您选择列表框图像时,滚动查看器将失去焦点. 因此,您可以将焦点设置为在MouseWheel/PreviewMouseWheel事件上滚动查看器,也可以如下所示手动滚动.
When you select Listbox image, focus is lost for scroll viewer. So, you can set focus to scroll viewer on MouseWheel/PreviewMouseWheel event or you can scroll manually like below.
myScroll.ScrollToVerticalOffset(myScroll.VerticalOffset + 10);
这篇关于WPF滚动不适用于ItemsControl内的ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文