代码之家  ›  专栏  ›  技术社区  ›  C. Hübscher

WPF:无法在数据网格中滚动

  •  5
  • C. Hübscher  · 技术社区  · 6 年前

    我有一个管理锦标赛的应用程序。我遇到滚动问题的视图如下所示: Group Phase View Screenshot

    现在我有一个问题,当鼠标在DataGrid上时,我无法滚动。例如,只要我尝试在扩展器上滚动,它就会工作(但一旦鼠标再次越过数据网格,滚动就会停止)。此外,ScrollViewer本身也可以按预期工作,例如,当我尝试使用滚动条本身滚动时。

    视图的构建如下所示。。。

    我有一个GroupPhaseView,看起来像这样(此视图包含ScrollViewer):

    <UserControl x:Class="TournamentApplication.UI.Tournament.GroupPhase.GroupPhaseView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:groupPhase="clr-namespace:TournamentApplication.UI.Tournament.GroupPhase"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsControl ItemsSource="{Binding GroupPhaseCategoryViewModels}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="groupPhase:GroupPhaseCategoryViewModel">
                        <groupPhase:GroupPhaseCategoryView />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
    

    此GroupPhaseView包含多个GroupPhaseCategoryView。一个GroupPhaseCategoryView如下所示:

    <UserControl x:Class="TournamentApplication.UI.Tournament.GroupPhase.GroupPhaseCategoryView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:groupPhase="clr-namespace:TournamentApplication.UI.Tournament.GroupPhase"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Expander Header="{Binding Category.Name}" IsExpanded="True">
            <ItemsControl ItemsSource="{Binding GroupPhaseGroupViewModels}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="groupPhase:GroupPhaseGroupViewModel">
                        <groupPhase:GroupPhaseGroupView />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Expander>
    </Grid>
    

    此GroupPhaseCategoryView包含多个GroupPhaseGroupView。它们包含两个导致滚动问题的数据网格:

    <UserControl x:Class="TournamentApplication.UI.Tournament.GroupPhase.GroupPhaseGroupView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <GroupBox Header="{Binding Group.Name}" ScrollViewer.CanContentScroll="True">
            <Grid ScrollViewer.CanContentScroll="True">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                </Grid.RowDefinitions>
                <DataGrid ScrollViewer.CanContentScroll="True" Grid.Row="0" Margin="0,10,0,30" ItemsSource="{Binding GroupGames}" AutoGenerateColumns="False" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Zeit" Binding="{Binding StartTime, StringFormat=HH:mm}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Platz" Binding="{Binding GameFieldName}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Heim-Team" Binding="{Binding Team1Name}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Gast-Team" Binding="{Binding Team2Name}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Heim-Tore" Binding="{Binding GoalsTeam1, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" IsReadOnly="False">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Gast-Tore" Binding="{Binding GoalsTeam2, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" IsReadOnly="False">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
                <DataGrid Grid.Row="1" ItemsSource="{Binding GroupTeams}" AutoGenerateColumns="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Team" Binding="{Binding Team.Name}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Spiele" Binding="{Binding PlayedGames.Count}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Niederlagen" Binding="{Binding LostGamesCount}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Unentschieden" Binding="{Binding DrawGamesCount}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Siege" Binding="{Binding WonGamesCount}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Tore" Binding="{Binding Goals}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Differenz" Binding="{Binding GoalDifference}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Punkte" Binding="{Binding Points}" IsReadOnly="True">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
        </GroupBox>
    </Grid>
    

    如您所见,我试图设置ScrollViewer。CanContentScroll=“true”属性在多个控件中实现预期结果,但不幸的是没有任何成功。。。

    1 回复  |  直到 6 年前
        1
  •  13
  •   MaSiMan    6 年前

    您的滚动事件由DataGrid处理,因此在DataGrid上滚动时,永远无法访问ScrollViewer。要解决此问题,请告诉数据网格将事件移交给父级(请参见 here 对于类似问题):

    订阅DataGrid上的PreviewMouseWheel

                <DataGrid PreviewMouseWheel="UIElement_OnPreviewMouseWheel" ScrollViewer.CanContentScroll="True" Grid.Row="0" Margin="40"
                          ItemsSource="{Binding GroupGames}" AutoGenerateColumns="False">
    

    。。。在回调中将事件传递给父级:

        private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                parent?.RaiseEvent(eventArg);
            }
        }