2012-05-21 4 views
5

Đây là những gì cây kiểm soát của tôi trông giống như:WPF Nested Scrollviewers - cho kiểm soát trở lại scollviewer mẹ

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

Sử dụng bánh xe chuột, điều khiển tự động truyền từ bố mẹ sang ScrollViewer đứa trẻ, nhưng khi tôi di chuyển đến cuối scrollviewer con điều khiển không quay trở lại scorllviewer cha mẹ. Làm thế nào để đạt được điều này?

Thiết bị mở rộng, lưới và trình xem cuộn được tạo động.

+0

Đồng thời xem câu trả lời cho [câu hỏi này] (http://stackoverflow.com/q/14348517/1925996). – piedar

Trả lời

10

Tôi gặp sự cố tương tự trong đơn đăng ký của mình. Tôi sửa nó bằng một tài sản depency mà sẽ bắt và vượt qua các sự kiện quá cha mẹ của mình. Điều này có thể được áp dụng cho bất kỳ điều khiển nào có cuộn trong đó. Nhưng đối với tôi, tôi không cần phải xác nhận nếu nó đã được ở cuối của cuộn để gửi cho cha mẹ của mình. Bạn sẽ chỉ cần thêm vào, trong phương thức OnValueChanged, một xác thực cho nếu cuộn ở cuối hoặc ở trên cùng để gửi cho cha mẹ của bạn.

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); 
    } 
} 
+0

Tôi đề nghị sử dụng hình ảnh cha mẹ trực quan 'VisualTreeHelper.GetParent (scrollview) như UIElement;' thay vì cha mẹ cây logic 'scrollview.Parent as UIElement;', tuy nhiên tôi phải thừa nhận rằng tôi không thể nhớ chính xác cấu trúc không thành công với cha mẹ hợp lý. – grek40