[英]Scrolling in a itemscontrol
I have an ItemsControl
template which contains another ItemsControl
and its template contains a DataGrid
. 我有一个
ItemsControl
模板,其中包含另一个ItemsControl
并且它的模板包含DataGrid
。
I want to be able to scroll through the first ItemsControl
, therefore I used the ScrollViewer
. 我希望能够滚动浏览第一个
ItemsControl
,因此我使用了ScrollViewer
。
The issue is that when the mouse is over any of the 2 ItemsControl
scrolling works, but when the mouse is over the DataGrid
it doesn't. 问题是,当鼠标悬停在两个
ItemsControl
滚动会起作用,但是当鼠标悬停在DataGrid
时,滚动不会起作用。
Would you know how I can fix this ? 您知道我该如何解决吗?
<TabItem Header="Summary">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="5" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="1"
VerticalScrollBarVisibility="Visible">
<ItemsControl Grid.Row="1"
ItemsSource="{Binding CheckResultViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock FontSize="20" FontWeight="ExtraBold">
<Run Text="{Binding CheckResults.Count, Mode=OneWay}" />
<Run>Warnings</Run>
</TextBlock>
</StackPanel>
<ItemsControl Grid.Row="1"
ItemsSource="{Binding CheckResults}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="5">
<DataGrid HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
AutoGenerateColumns="True"
AutoGeneratingColumn="DetailsDataGrid_AutoGeneratingColumn"
CanUserResizeRows="False"
IsReadOnly="True"
ItemsSource="{Binding ErrorData}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</TabItem>
DataGrid have ScrollViewer in it's default template. DataGrid的默认模板中包含ScrollViewer。 So, when mouse is over DataGrid, scroll event is eaten up by dataGrid's ScrollViewer and thus doesn't bubbles up to parent ScrollViewer .
因此,当鼠标悬停在DataGrid上时, 滚动事件被dataGrid的ScrollViewer吞噬,因此不会冒泡到父级ScrollViewer 。
In case you want outer ScrollViewer to scroll you can hook tunnelling PreviewMouseWheel
event and scroll manually: 如果您希望外部ScrollViewer滚动,则可以挂接隧道的
PreviewMouseWheel
事件并手动滚动:
XAML : XAML :
<ScrollViewer Grid.Row="1"
VerticalScrollBarVisibility="Visible"
PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
<ItemsControl Grid.Row="1"
ItemsSource="{Binding CheckResultViewModels}">
...........
Code behind : 后面的代码 :
private void ScrollViewer_PreviewMouseWheel(object sender,
MouseWheelEventArgs e)
{
ScrollViewer scrollViewer = (ScrollViewer)sender;
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.