滚动项目控件

时间:2014-07-31 15:46:47

标签: wpf xaml datagrid wpfdatagrid scrollviewer

我有一个ItemsControl模板,其中包含另一个ItemsControl,其模板包含DataGrid

我希望能够滚动浏览第一个ItemsControl,因此我使用了ScrollViewer

问题是,当鼠标悬停在2 ItemsControl个滚动中的任何一个上时,但当鼠标位于DataGrid之上时,它不会。

你知道如何解决这个问题吗?

<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>

1 个答案:

答案 0 :(得分:5)

DataGrid在其默认模板中包含ScrollViewer。因此,当鼠标在DataGrid上时, 滚动事件被dataGrid的ScrollViewer占用,因此不会冒泡到父ScrollViewer

如果您想要滚动外部ScrollViewer,可以挂钩隧道 PreviewMouseWheel 事件并手动滚动:

<强> XAML

<ScrollViewer Grid.Row="1"
              VerticalScrollBarVisibility="Visible"
              PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
    <ItemsControl Grid.Row="1"
                  ItemsSource="{Binding CheckResultViewModels}">
        ...........

代码

private void ScrollViewer_PreviewMouseWheel(object sender, 
                                            MouseWheelEventArgs e)
{
    ScrollViewer scrollViewer = (ScrollViewer)sender;
    scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta);
}