Silverlight 3 datagrid:如何添加滚轮支持?

时间:2010-02-16 13:33:45

标签: silverlight silverlight-toolkit

silverlight 3工具包中的Datagrid没有响应滚轮(鼠标滚轮)。有没有办法获得滚轮的支持?

2 个答案:

答案 0 :(得分:3)

以下是我正在使用的行为。下面就是你如何将它附加到xaml中的datagrid。

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Automation.Peers;
using System.Windows.Interactivity;
using System.Windows.Automation.Provider;
using System.Windows.Automation;
using System.Windows.Data;

namespace GLS.Gui.Helper.Behaviors
{
    public class MouseWheelScrollBehavior : Behavior<Control>
    {
        /// <summary>
        /// Gets or sets the peer.
        /// </summary>
        /// <value>The peer.</value>
        private AutomationPeer Peer { get; set; }

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
        protected override void OnAttached()
        {
            this.Peer = FrameworkElementAutomationPeer.FromElement(this.AssociatedObject);

            if (this.Peer == null)
                this.Peer = FrameworkElementAutomationPeer.CreatePeerForElement(this.AssociatedObject);

            this.AssociatedObject.MouseWheel += new MouseWheelEventHandler(AssociatedObject_MouseWheel);
            base.OnAttached();
        }

        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
        /// </summary>
        /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
        protected override void OnDetaching()
        {
            this.AssociatedObject.MouseWheel -= new MouseWheelEventHandler(AssociatedObject_MouseWheel);
            base.OnDetaching();
        }

        /// <summary>
        /// Handles the MouseWheel event of the AssociatedObject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseWheelEventArgs"/> instance containing the event data.</param>
        void AssociatedObject_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            //Do not handle already handled events
            if (e.Handled)
                return;

            this.AssociatedObject.Focus();

            int direction = Math.Sign(e.Delta);

            ScrollAmount scrollAmount =
                (direction < 0) ? ScrollAmount.SmallIncrement : ScrollAmount.SmallDecrement;

            if (this.Peer != null)
            {
                IScrollProvider scrollProvider =
                    this.Peer.GetPattern(PatternInterface.Scroll) as IScrollProvider;

                bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;

                if (scrollProvider != null && scrollProvider.VerticallyScrollable && !shiftKey)
                {
                    scrollProvider.Scroll(ScrollAmount.NoAmount, scrollAmount);
                    e.Handled = true;
                }
                else if (scrollProvider != null && scrollProvider.VerticallyScrollable && shiftKey)
                {
                    scrollProvider.Scroll(scrollAmount, ScrollAmount.NoAmount);
                    e.Handled = true;
                }


            }
        }
    }


}

如何附加行为:

<data:DataGrid>
            <i:Interaction.Behaviors>
                <b:MouseWheelScrollBehavior />
            </i:Interaction.Behaviors>
        </data:DataGrid>

答案 1 :(得分:0)

另一种方法(非常基本的例子):
XAML:

<Grid x:Name="LayoutRoot">
    <data:DataGrid x:Name="dg" Height="100">
        <ScrollViewer.VerticalScrollBarVisibility>true</ScrollViewer.VerticalScrollBarVisibility>
    </data:DataGrid>


CS:

public partial class MainPage : UserControl
{
    IList<Person> list = new List<Person>();
    public MainPage()
    {
        InitializeComponent();
        list.Add(new Person("Pieter1","Nijs"));
        list.Add(new Person("Pieter2", "Nijs"));
        list.Add(new Person("Pieter3", "Nijs"));
        list.Add(new Person("Pieter4", "Nijs"));
        list.Add(new Person("Pieter5", "Nijs"));
        list.Add(new Person("Pieter6", "Nijs"));
        list.Add(new Person("Pieter7", "Nijs"));
        list.Add(new Person("Pieter8", "Nijs"));
        dg.ItemsSource = list;
        dg.MouseWheel += new MouseWheelEventHandler(dg_MouseWheel);
    }

    void dg_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta < 0)
        {
            dg.ScrollIntoView(list[dg.SelectedIndex + 2], null);
        }
        else
        {
            dg.ScrollIntoView(list[dg.SelectedIndex - 2], null);
        }
    }
}

所以,我在这里做的很简单! 我向DataGrid MouseWheel - 事件添加了一个EventHandler。在那个处理程序中,我检索e.Delta(这是自上次以来车轮已经改变的数量)所以我知道用户是向上滚动(正三角洲)还是向下滚动(负三角洲)。然后我调用DataGrid的ScrollIntoView - 方法,在那里我可以指定Grid应该滚动到哪一行。
如上所述,这是一个非常基本的例子!这只是为了向您展示它是如何工作的!你应该添加额外的逻辑,以确保你不会退出!