[英]WPF: Cannot scroll in DataGrids
I have an application where I manage a tournament. 我有一个应用程序,我管理锦标赛。 The view which I have a scrolling issue looks like that: Group Phase View Screenshot
我有一个滚动问题的视图看起来像: Group Phase View Screenshot
Now I have the problem that I cannot scroll when the mouse is over the DataGrid. 现在我遇到的问题是当鼠标悬停在DataGrid上时我无法滚动。 As soon as I try to scroll for example on the Expander - it works (but as soon as the mouse then gets over a DataGrid again, the scrolling stops).
一旦我尝试在扩展器上滚动示例 - 它可以工作(但是一旦鼠标再次越过DataGrid,滚动停止)。 Also the ScrollViewer itself works as expected, for example when I am trying to scroll with the scrollbar itself.
此外,ScrollViewer本身也按预期工作,例如当我尝试使用滚动条本身滚动时。
The view is build as following... 视图构建如下......
I have a GroupPhaseView that looks like that (this view contains the 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>
This GroupPhaseView contains multiple GroupPhaseCategoryView's. 这个GroupPhaseView包含多个GroupPhaseCategoryView。 One GroupPhaseCategoryView looks like that:
一个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>
This GroupPhaseCategoryView contains multiple GroupPhaseGroupView's. 此GroupPhaseCategoryView包含多个GroupPhaseGroupView。 They contain two DataGrid which are leading to the scrolling issue:
它们包含两个导致滚动问题的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>
As you see I tried to set the ScrollViewer.CanContentScroll = "true" property in multiple controls to achieve the expected result - but unfortunately without any success... 如您所见,我尝试在多个控件中设置ScrollViewer.CanContentScroll =“true”属性以实现预期结果 - 但遗憾的是没有任何成功...
Your scroll event is handled by the DataGrid, and can thus never reach the ScrollViewer while you are scrolling on the DataGrid. 您的滚动事件由DataGrid处理,因此当您在DataGrid上滚动时,它永远不会到达ScrollViewer。 To solve this, tell the data grid to hand over the event to the parent (see here for a similar question):
要解决此问题,请告诉数据网格将事件移交给父级(有关类似问题,请参阅此处 ):
Subscribe to PreviewMouseWheel on the DataGrid 订阅DataGrid上的PreviewMouseWheel
<DataGrid PreviewMouseWheel="UIElement_OnPreviewMouseWheel" ScrollViewer.CanContentScroll="True" Grid.Row="0" Margin="40"
ItemsSource="{Binding GroupGames}" AutoGenerateColumns="False">
...and in the callback hand the event to the parent: ...并在回调中将事件交给父母:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.