Mouse scroll not working in a scroll viewer with a wpf datagrid and additional UI elements

前端 未结 10 1433
醉话见心
醉话见心 2021-02-03 18:41

I am trying to figure out how to get the mouse scroll working on a wpf window with a scrollviewer and a datagrid within it. The WPF and C# code is below



        
相关标签:
10条回答
  • 2021-02-03 18:42

    I'm assuming the DataGrid does not need to scroll - Set the VerticalScrollBar="None" on the DataGrid.

    The DataGrid swallows the mouse scroll event.

    What I found is to use the PreviewMouseWheel event to scroll the container that you want to scroll. You will need to name the scrollviewer to change the Vertical offset.

    private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
           scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset-e.Delta);
        }
    
    0 讨论(0)
  • 2021-02-03 18:43

    Here is a larger example of creating a WPF behavior that scrolls the DataGrid.

    First define the following base class for gluing together framework element with behavior class and its behavior implementation.

    using System.Diagnostics.CodeAnalysis;
    using System.Windows;
    using System.Windows.Input;
    
    namespace SomeAcme.Client.Infrastructure
    {
    
    
        /// <summary>
        /// Behavior handler class for creating WPF behaviors.
        /// </summary>  
         [ExcludeFromCodeCoverage] 
        public class BehaviorHandler<TAssociatedObject, TBehaviorClass> 
            where TAssociatedObject: DependencyObject
            where TBehaviorClass : class, IAssociationBehavior, new()
        {
    
            public BehaviorHandler()
            {
            }
    
            public static TBehaviorClass GetBehavior(DependencyObject obj)
            {
                if (obj == null)
                    return null; 
    
                return (TBehaviorClass)obj.GetValue(BehaviorProperty);
            }
    
            public static void SetBehavior(DependencyObject obj, TBehaviorClass value)
            {
                if (obj != null)
                {
                    obj.SetValue(BehaviorProperty, value);
                }
            }
    
            // Using a DependencyProperty as the backing store for Behavior.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty BehaviorProperty =
                DependencyProperty.RegisterAttached("Behavior", typeof(TBehaviorClass), typeof(object), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
    
    
            public void FindOrCreateBehaviorOnDemand(DependencyObject dependencyObject)
            {
                 //Apply the behavior 
                 TBehaviorClass behavior = FindOrCreateBehavior(dependencyObject);
                 if (behavior != null)
                 {
                     dependencyObject.SetValue(BehaviorProperty, behavior); 
                 }
            }
    
            public TBehaviorClass FindOrCreateBehavior(DependencyObject dependencyObject)
            {
                if (dependencyObject == null)
                    return null;
                var associatedObject = dependencyObject;
                if (associatedObject == null)
                    return null;
                var behavior = dependencyObject.GetValue(BehaviorProperty) as TBehaviorClass;
                if (behavior == null)
                {
                    var behaviorAssociated = new TBehaviorClass();
                    if (behaviorAssociated == null)
                        return null;
                    behaviorAssociated.InitializeAssociation(associatedObject, AssociatedCommands);
                    return behaviorAssociated;
                }
                else
                {
                    return behavior;
                }
            } //TBehaviorClass FindOrCreateBehavior 
    
            /// <summary>
            /// Set the associated commands to pass into the WPF behavior, if desired
            /// </summary>
            public ICommand[] AssociatedCommands { get; set; }
    
        }
    
    }
    

    This looks a bit more complex, but it simplifies boiler plate coding when it comes to gluing together the behavior class with the framework element. In this particular case we will not need to use an associated command.

    Our scroll viewer behavior then looks like this:

        using SomeAcme.Client.Infrastructure;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Input;
    
        namespace SomeAcme.Client.Infrastructure
        {
    
            public static class ScrollViewerMouseWheelScrollBehavior
            {
    
    
                    public static string GetUseMouseScrollForScrollViewer(DependencyObject obj)
                    {
                        if (obj != null)
                        {
                            return (string)obj.GetValue(UseMouseScrollForScrollViewerProperty);
                        }
                        else
                            return null;
                    }
    
                    public static void SetUseMouseScrollForScrollViewer(DependencyObject obj, string value)
                    {
                        if (obj != null)
                        {
                            obj.SetValue(UseMouseScrollForScrollViewerProperty, value);
                        }
                    }
    
    
     public static readonly DependencyProperty UseMouseScrollForScrollViewerCommandProperty =
                        DependencyProperty.RegisterAttached("UseScrollForScrollViewer", typeof(string), typeof(ScrollViewerMouseWheelScrollBehavior),
                            new PropertyMetadata(new PropertyChangedCallback(OnUseScrollForScrollViewerSet)));
    
                    public static void OnUseScrollForScrollViewerSet(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
                    {
                        bool useScroll;
                        bool.TryParse(eventArgs.NewValue?.ToString(), out useScroll);
                        if (useScroll)
                        {
                            var behaviorHandler =
                                new BehaviorHandler<ScrollViewer, ScrollViewerMouseScrollBehaviorImplementation>();
                            //behaviorHandler.AssociatedCommands = 
                            //    new Microsoft.Practices.Prism.Commands.DelegateCommand<object>[] { (Microsoft.Practices.Prism.Commands.DelegateCommand<object>)eventArgs.NewValue };
                            behaviorHandler.FindOrCreateBehaviorOnDemand(dependencyObject);
                        }
                    }
    
                }
    
        }
    
        namespace SomeAcme.Client.Infrastructure
        {
            public class ScrollViewerMouseScrollBehaviorImplementation : IAssociationBehavior
            {
                public void InitializeAssociation(DependencyObject associatedObject, params ICommand[] commands)
                {
                    //TODO: Add commands to associate
                    var scrollViewer = associatedObject as ScrollViewer;
                    if (scrollViewer != null)
                    {
                        scrollViewer.PreviewMouseWheel += ScrollViewer_PreviewMouseWheel;
                    }
                }
    
                private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
                {
                    var scrollViewer = sender as ScrollViewer;
                    if (scrollViewer != null)
                    {
                        scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta);
                    }
                }
            }
        }
    

    In XAML we then can first define the namespaces:

    xmlns:local="clr-namespace:SomeAcme.Client.Infrastructure"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

    Finally we are ready to use the mouse wheel scroll by using the WPF behavior in the XAML.

    ..

     <TabControl Grid.Row="1">
            <TabItem Header="Skjemafelter">
                <ScrollViewer Height="700" local:ScrollViewerMouseWheelScrollBehavior.UseMouseScrollForScrollViewer="{x:Static sys:Boolean.TrueString}">
                    <ListView x:Name="ColumnsListView" ItemsSource="{Binding CurrentFields}">
                        <ListView.View>
                            <GridView>
    

    I have tested and verified that this approach works. For developers working with a WPF app, utilizing WPF behaviors keeps amount of code in code behind still to the minimum bits and staying faitfully to the MVVM approach.

    0 讨论(0)
  • 2021-02-03 18:46

    Guys I`ve seen most of the solution posted over here and fundamentally all of them are right, but I think there is an easiest and more elegant syntax that we could apply.

    Assuming that we don't need to scroll with our data grid and instead we would like to scroll with our MainWindow

    fallowing "Dave" and "Vladim Tofan" answer

    Private Void Scrlll(Object sebder, MouseWheelEventArgs e)
    {
       var windows = (Window.GetWindow(this) as MainWindow).MainScroll;
       windows.ScrollToVerticalOffset(windows.VerticalOffset - e.Delta);
    }
    

    Sorry, for bad english.

    0 讨论(0)
  • 2021-02-03 18:46

    I found that ScrollViewer can't be concatenated, which means if it is concatenated like in your case the Grid starts under the ScrollViewer tag and in the Grid we have DataGrid and in the DataGrid again the ScrollViewer property has been set. i.e.

      <Grid>
         <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="45" />
            <RowDefinition Height="100*" />
            <RowDefinition Height="105" />
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
         </Grid.ColumnDefinitions>
    
         <Label Grid.Row="0"
                Grid.Column="0"
                Margin="10,0,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Center"
                FontWeight="Bold"
                Content="Vessel: " />
         <TextBox Height="30"
                  Width="300"
                  Margin="70,0,0,0"
                  HorizontalAlignment="Left"
                  BorderThickness="1,1,1,1"
                  IsReadOnly="True"
                  Name="txtVessel" />
    
         <Label  Grid.Row="0"
                 Grid.Column="2"
                 Margin="0,0,185,0"
                 HorizontalAlignment="Right"
                 VerticalAlignment="Center"
                 FontWeight="Bold"
                 Content="Month:" />
    
         <StackPanel Orientation="Horizontal"
                     Grid.Row="0"
                     Grid.Column="2"
                     Margin="0,0,0,0"
                     HorizontalAlignment="Right">
    
            <ComboBox BorderThickness="2"
                      HorizontalAlignment="Right"
                      Name="CmbMonth"
                      VerticalAlignment="Center"
                      Width="90" />
            <ComboBox BorderThickness="2"
                      HorizontalAlignment="Right"
                      Margin="5,0,0,0"
                      Name="CmbYear"
                      VerticalAlignment="Center"
                      Width="90" />
    
         </StackPanel>
    
         <Grid Grid.Row="1"
               Grid.ColumnSpan="2">
            <Grid.RowDefinitions>
               <RowDefinition Height="45" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="30" />
               <ColumnDefinition Width="220" />
               <ColumnDefinition Width="80" />
               <ColumnDefinition Width="80" />
               <ColumnDefinition Width="80" />
               <ColumnDefinition Width="80" />
               <ColumnDefinition Width="120" />
               <ColumnDefinition Width="120" />
               <ColumnDefinition Width="140*" />
            </Grid.ColumnDefinitions>
    
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="0" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="1" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="2" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="3" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="4" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="5" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="6" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="7" />
            <Border BorderBrush="Black"
                    BorderThickness="0,1,1,1"
                    Grid.Row="0"
                    Grid.Column="8" />
    
            <Label  Grid.Row="0"
                    Grid.Column="1"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="Item" />
    
            <Label  Grid.Row="0"
                    Grid.Column="2"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="Maker" />
    
            <Label  Grid.Row="0"
                    Grid.Column="3"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="Model" />
    
            <Label  Grid.Row="0"
                    Grid.Column="4"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content=" Part No.&#x0a;Serial No." />
    
            <Label  Grid.Row="0"
                    Grid.Column="5"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="Condition" />
    
            <Label  Grid.Row="0"
                    Grid.Column="6"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="   Onboard&#x0a; Calibr/Test" />
    
            <Label  Grid.Row="0"
                    Grid.Column="7"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="     Shore&#x0a; Callibration" />
    
            <Label  Grid.Row="0"
                    Grid.Column="8"
                    Margin="0,0,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="Remarks" />
    
         </Grid>
    
         <Border Grid.Row="2"
                 Grid.ColumnSpan="2">
            <ScrollViewer Grid.Row="2"
                          Grid.ColumnSpan="2"
                          CanContentScroll="True"
                          HorizontalScrollBarVisibility="Disabled"
                          VerticalScrollBarVisibility="Auto"
                          Name="ScrollViewer3"
                          Margin="0,0,0,0">
               <Grid Name="grdOnBoardCalibrationRecord"
                     Margin="0,0,0,0">
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="30"></ColumnDefinition>
                     <ColumnDefinition Width="220"></ColumnDefinition>
                     <ColumnDefinition Width="80"></ColumnDefinition>
                     <ColumnDefinition Width="80"></ColumnDefinition>
                     <ColumnDefinition Width="80"></ColumnDefinition>
                     <ColumnDefinition Width="80"></ColumnDefinition>
                     <ColumnDefinition Width="120"></ColumnDefinition>
                     <ColumnDefinition Width="120"></ColumnDefinition>
                     <ColumnDefinition Width="140*"></ColumnDefinition>
                  </Grid.ColumnDefinitions>
                  <Border Grid.Column="0"
                          BorderThickness="1,0,1,1"
                          BorderBrush="Black"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="1"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="2"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="3"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="4"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="5"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="6"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="7"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
                  <Border Grid.Column="8"
                          BorderThickness="0,1,1,1"
                          Grid.RowSpan="26"></Border>
               </Grid>
            </ScrollViewer>
         </Border>
         <Grid Grid.Row="3"
               Grid.ColumnSpan="2">
            <Grid.RowDefinitions>
               <RowDefinition Height="30"></RowDefinition>
               <RowDefinition Height="30"></RowDefinition>
               <RowDefinition Height="40"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
    
            <TextBox Grid.Row="0"
                     Grid.Column="0"
                     Height="30"
                     Width="300"
                     TextAlignment="Center"
                     Background="Gray"
                     IsReadOnly="True"
                     Margin="0,0,0,0"
                     HorizontalAlignment="Right"
                     VerticalAlignment="Bottom"
                     BorderThickness="1,1,1,1"
                     Name="txtChiefEngineer" />
            <Label  Grid.Row="1"
                    Grid.Column="1"
                    Margin="0,0,100,0"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Content="Chief Engineer" />
            <StackPanel Orientation="Horizontal"
                        Grid.Row="2"
                        Margin="0,0,0,0">
               <Label Name="lblonshorecomment"
                      Content=" Shore Comment : "
                      HorizontalAlignment="Center"
                      Margin="5,0,0,0"
                      FontWeight="Bold"
                      VerticalAlignment="Center"
                      FontFamily="Calibri"
                      FontStyle="Normal"
                      FontSize="14"></Label>
               <TextBox  BorderThickness="1"
                         FontWeight="Normal"
                         IsReadOnly="True"
                         Height="44"
                         Width="878"
                         TextWrapping="Wrap"
                         AcceptsReturn="True"
                         HorizontalAlignment="left"
                         Margin="0,0,0,0"
                         Name="txtShoreComment"
                         VerticalAlignment="Center" />
            </StackPanel>
    
         </Grid>
      </Grid>
    

    0 讨论(0)
  • 2021-02-03 18:54

    An improvement to Don B's solution is to avoid using ScrollToVerticalOffset.

    scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
    

    VerticalOffset - Delta results in a pretty jarring experience. The ScrollViewer puts a lot of thought into making the movement smoother than that. I think it also scales the delta down based on dpi and other factors...

    I found it's better to catch and handle the PreviewMouseWheelEvent and send a MouseWheelEvent to the intended ScrollViewer. My version of Don B's solution looks like this.

    private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
        eventArg.RoutedEvent = UIElement.MouseWheelEvent;
        eventArg.Source = e.Source;
    
        ScrollViewer scv = (ScrollViewer)sender;
        scv.RaiseEvent(eventArg);
        e.Handled = true;
    }
    
    0 讨论(0)
  • 2021-02-03 18:56

    @fjch1997, I used your solution and it works pretty well. There is only one problem which I found. It's connected with the comment of @Vadim Tofan:

    For the case when the scroll viewer has as children other scrollable controls and a data grid, then that requires that the event shouldn't be marked as handled at the end of the event handler for the main scroll viewer so it could be catched also in the inner scrollable controls, however that has the side effect that when the scroll should occur only on the inner scrollable control, it will also occur on the main scroll viewer.

    I also tried to remove e.Handled = true statement, but then the effect is not nice - both scrolls are moved in the same time. So, finally I enhanced a little bit event handler method to the following one:

    private static void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
    {
        ScrollViewer scrollViewer = (ScrollViewer)sender;
        FrameworkElement origicalControlSender = e.OriginalSource as FrameworkElement;
    
        ScrollViewer closestScrollViewer = origicalControlSender.GetParent<ScrollViewer>();
    
        if (closestScrollViewer != null && !ReferenceEquals(closestScrollViewer, scrollViewer))
        {
            return;
        }
    
        scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta);
        e.Handled = true;
    }
    
    public static T GetParent<T>(this FrameworkElement control)
        where T : DependencyObject
    {
        FrameworkElement parentElement = control?.Parent as FrameworkElement;
    
        if (parentElement == null)
        {
            return null;
        }
    
        T parent = parentElement as T;
    
        if (parent != null)
        {
            return parent;
        }
    
        return GetParent<T>(parentElement);
    }
    

    This now prevents the outside scroller to scroll in case inner ScrollViewer exists.

    0 讨论(0)
提交回复
热议问题