简体   繁体   中英

Grid inside Scrollview not scrolling by mouse wheel

I have a column in a grid which contains 3 GroupBoxe s. The last GroupBox has a Grid containing two elements: a Button and a TreeView (vertical). The TreeView should have a dynamically height because it contains elements which can be expanded/collapsed.

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.

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.

You have to "redirect" the PreviewMouseWheel event to the parent :

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM