c# - WPF:无法在 DataGrids 中滚动

标签 c# wpf datagrid scrollviewer

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

现在我遇到了当鼠标悬停在 DataGrid 上时无法滚动的问题。例如,一旦我尝试在 Expander 上滚动 - 它就可以工作(但是一旦鼠标再次越过 DataGrid,滚动就会停止)。 ScrollViewer 本身也按预期工作,例如当我尝试使用滚动条本身滚动时。

View 构建如下...

我有一个看起来像这样的 GroupPhaseView(此 View 包含 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。它们包含两个导致滚动问题的 DataGrid:

<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"属性以实现预期结果 - 但遗憾的是没有任何成功......

最佳答案

您的滚动事件由 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);
        }
    }

关于c# - WPF:无法在 DataGrids 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49462930/

相关文章:

c# - 按字段复制对象

c# - 想要在单击按钮时动态地在网格中添加文本 block

c# - WPF Datagrid 未更新

c# - WPF - 通过 TableAdapter、DataContext/Dataview 和 DataGrid 将数据库编辑到 TextBox

.net - 识别整个 DataGrid 的失去焦点事件,而不是其控件失去焦点

c# - IEnumerable 是否必须使用 Yield 才能延迟

c# - IEnumerable<T> 与数组

c# - 在不指定元数据端点的情况下使用 OpenIdConnect

c# - 禁用与当前 View 对应的当前按钮wpf c#

wpf - C# WPF 使用点在墨水 Canvas 上绘制描边