[英]Grid inside Scrollview not scrolling by mouse wheel
I have a column in a grid which contains 3 GroupBoxe
s. 我在包含3
GroupBoxe
的网格中有一列。 The last GroupBox
has a Grid
containing two elements: a Button
and a TreeView
(vertical). 最后一个
GroupBox
具有一个包含两个元素的Grid
:一个Button
和一个TreeView
(垂直)。 The TreeView
should have a dynamically height because it contains elements which can be expanded/collapsed. TreeView
应该具有动态高度,因为它包含可以扩展/折叠的元素。
The window which contais all elements can be resized by the user. 包含所有元素的窗口可由用户调整大小。
If the window is to small you can't see all groupboxes so I need a Scrollbar. 如果窗口太小,您将看不到所有分组框,因此我需要滚动条。 I can scroll down but if I want to scroll inside the
TreeView
by the mouse wheel nothing happens. 我可以向下滚动,但是如果要通过鼠标滚轮在
TreeView
滚动,则不会发生任何事情。
This is my code: 这是我的代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GroupBox Header="Test 1" Grid.Row="0">...</GroupBox>
<GroupBox Header="Test 2" Grid.Row="1">...</GroupBox>
<GroupBox Header="Test 3" Grid.Row="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox Content="All" Grid.Row="0"/>
<TreeView x:Name="NameTree"
Grid.Row="1"
ItemsSource="{Binding Names}"/>
</Grid>
</GroupBox>
</Grid>
A workaround could be to set the 3rd row to a fixed size instead of using the *. 一种解决方法是将第三行设置为固定大小,而不使用*。 In this case I will two scrollbars (the ScrollViewer and the TreeViews Scrollbar, but I want a dynamically height of the 3rd groupbox.
在这种情况下,我将使用两个滚动条(ScrollViewer和TreeViews滚动条,但是我希望动态设置第三个分组框的高度。
You have to "redirect" the PreviewMouseWheel event to the parent : 您必须将PreviewMouseWheel事件“重定向”到父级:
<TreeView PreviewMouseWheel="TreeView_MouseWheel" >
<TreeViewItem Header="North America">
<TreeViewItem Header="USA"></TreeViewItem>
<TreeViewItem Header="Canada"></TreeViewItem>
<TreeViewItem Header="Mexico"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="South America">
<TreeViewItem Header="Argentina"></TreeViewItem>
<TreeViewItem Header="Brazil"></TreeViewItem>
<TreeViewItem Header="Uruguay"></TreeViewItem>
</TreeViewItem>
Code behind : 后面的代码:
private void TreeView_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (!e.Handled)
{
e.Handled = true;
var eventArg = new MouseWheelEventArgs(
e.MouseDevice, e.Timestamp, e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
var parent = ((Control)sender).Parent as UIElement;
parent.RaiseEvent(eventArg);
}
}
WPF Remove ScrollViewer from TreeView WPF从TreeView移除ScrollViewer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.