7

这是我的控制树的样子:

<window>
 <scrollviewer>
  <expander>
   <scrollviewer>
    <grid>
    </grid>
   </scrollviewer>
  </expander>
  <expander>
   <scrollviewer>
    <grid>
    </grid>
   </scrollviewer>
  </expander>
 </scrollviewer>
</window>

使用鼠标滚轮,控件会自动从父滚动查看器传递到子滚动查看器,但是当我滚动到子滚动查看器的末尾时,控件不会传递回父滚动查看器。我如何实现这一目标?

扩展器、网格和滚动查看器是动态生成的。

4

2 回答 2

11

我在我的应用程序中遇到了类似的问题。我通过一个依赖属性来纠正它,该属性将捕获并将事件传递给他的父母。这可以应用于任何具有滚动条的控件。但对我来说,我不需要验证它是否在卷轴的末尾发送给他的父母。您只需要在 OnValueChanged 方法中添加验证滚动是在末尾还是在顶部以发送给他的父级。

using System.Windows.Controls;

public static class SendMouseWheelToParent
{
   public static readonly DependencyProperty ScrollProperty
     = DependencyProperty.RegisterAttached("IsSendingMouseWheelEventToParent",
        typeof(bool),
        typeof(SendMouseWheelToParent),
        new FrameworkPropertyMetadata(OnValueChanged));

   /// <summary>
   /// Gets the IsSendingMouseWheelEventToParent for a given <see cref="TextBox"/>.
   /// </summary>
   /// <param name="control">
   /// The <see cref="TextBox"/> whose IsSendingMouseWheelEventToParent is to be retrieved.
   /// </param>
   /// <returns>
   /// The IsSendingMouseWheelEventToParent, or <see langword="null"/>
   /// if no IsSendingMouseWheelEventToParent has been set.
   /// </returns>
   public static bool? GetIsSendingMouseWheelEventToParent(Control control)
   {
       if (control == null)
           throw new ArgumentNullException("");

       return control.GetValue(ScrollProperty) as bool?;
   }

   /// <summary>
   /// Sets the IsSendingMouseWheelEventToParent for a given <see cref="TextBox"/>.
   /// </summary>
   /// <param name="control">
   /// The <see cref="TextBox"/> whose IsSendingMouseWheelEventToParent is to be set.
   /// </param>
   /// <param name="IsSendingMouseWheelEventToParent">
   /// The IsSendingMouseWheelEventToParent to set, or <see langword="null"/>
   /// to remove any existing IsSendingMouseWheelEventToParent from <paramref name="control"/>.
   /// </param>
   public static void SetIsSendingMouseWheelEventToParent(Control control, bool? sendToParent)
   {
       if (control == null)
           throw new ArgumentNullException("");

       control.SetValue(ScrollProperty, sendToParent);
   }

   private static void OnValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
   {
       var scrollViewer = dependencyObject as Control;
       bool? IsSendingMouseWheelEventToParent = e.NewValue as bool?;
       scrollViewer.PreviewMouseWheel -= scrollViewer_PreviewMouseWheel;

       if (IsSendingMouseWheelEventToParent != null && IsSendingMouseWheelEventToParent != false)
       {
         scrollViewer.SetValue(ScrollProperty, IsSendingMouseWheelEventToParent);
         scrollViewer.PreviewMouseWheel += scrollViewer_PreviewMouseWheel;
       }
   }


   private static void scrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
   {
       var scrollview = sender as Control;

       var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
       eventArg.RoutedEvent = UIElement.MouseWheelEvent;
       eventArg.Source = sender;
       var parent = scrollview.Parent as UIElement;
       parent.RaiseEvent(eventArg);
   }
}
于 2012-08-22T19:48:23.587 回答
1

受 Janick 的回答和其他一些人的启发,我有一个实现,当内部的 ScrollViewers(包括来自 ListView、ListBox、DataGrid)滚动超过它们的顶部/底部时滚动祖先 ScrollViewers。该代码在我对一个非常相似的问题的回答中

于 2020-04-15T01:03:24.080 回答