PropertyTools / PropertyTools

Custom controls for WPF: PropertyGrid, DataGrid, multi-select TreeView, ColorPicker and more
MIT License
1.12k stars 182 forks source link

How can I make the PropertyGrid control respond to mouse wheel scrolling? #341

Open zhanggaolei001 opened 3 months ago

zhanggaolei001 commented 3 months ago

Here is the demo code:

    x:Class="PropertyGridDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PropertyGridDemo"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="450"
    Width="800"
    xmlns:pt="http://propertytools.org/wpf">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <ListView
        Background="Aqua">
        <ListViewItem >
            <Ellipse
                Width="100"
                Height="200"
                Fill="AliceBlue"></Ellipse>
        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm1}"></pt:PropertyGrid>
        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm2}"></pt:PropertyGrid>

        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm3}"></pt:PropertyGrid>

        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm4}"></pt:PropertyGrid>

        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm5}"></pt:PropertyGrid>

        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm6}"></pt:PropertyGrid>

        </ListViewItem>
        <ListViewItem>
            <pt:PropertyGrid
                SelectedObject="{Binding Vm7}"></pt:PropertyGrid>

        </ListViewItem>
    </ListView>
</Window>

image

In my project, the view model and the view are more complicated, but they have the same problem. I can scroll vertically with the mouse wheel in the Ellipse or empty space, but the PropertyGrid zone does not work.How can I make the PropertyGrid control respond to mouse wheel scrolling?

zhanggaolei001 commented 3 months ago

Ok, I have handled the PreviewMouseWheel event to implement this functionality temporarily

       private void PropertyGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
       {
           var sc = FindParentScrollViewer(this);
           if (sc != null)
           {

               sc.ScrollToVerticalOffset(sc.VerticalOffset - e.Delta);
           }
           e.Handled = true;
       }
       public ScrollViewer FindParentScrollViewer(DependencyObject child)
       {

           if (child == null) return null;

           while (child != null && !(child is ScrollViewer))
           {
               child = VisualTreeHelper.GetParent(child);
           }
           return child as ScrollViewer;
       }

Is there any way better?

github-actions[bot] commented 1 month ago

Stale issue message