c#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.Threading;

namespace Common
{
    public class MouseClickManager
    {
        public event MouseButtonEventHandler Click;
        public event MouseButtonEventHandler DoubleClick;

        private bool Clicked { get; set; }
        
        public Control Control { get; set; }

        public int Timeout { get; set; }

        public MouseClickManager(Control control, int timeout)
        {
            this.Clicked = false;
            this.Control = control;
            this.Timeout = timeout;
        }

        public void HandleClick(object sender, MouseButtonEventArgs e)
        {
            lock (this)
            {
                if (this.Clicked)
                {
                    this.Clicked = false;
                    OnDoubleClick(sender, e);
                }
                else
                {
                    this.Clicked = true;
                    ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
                    Thread thread = new Thread(threadStart);
                    thread.Start(e);
                }
            }
        }

        private void ResetThread(object state)
        {
            Thread.Sleep(this.Timeout);

            lock (this)
            {
                if (this.Clicked)
                {
                    this.Clicked = false;
                    OnClick(this, (MouseButtonEventArgs)state);
                }
            }
        }

        private void OnClick(object sender, MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = Click;

            if (handler != null)
                this.Control.Dispatcher.BeginInvoke(handler, sender, e);
        }

        private void OnDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = DoubleClick;

            if (handler != null)
                handler(sender, e);
        }
    }
}
View Code
<sdk:DataGrid Grid.Row="0" AutoGenerateColumns="False" SelectionMode="Single"  IsReadOnly="True" x:Name="dataGrid" LoadingRow="DataGrid_LoadingRow" />

private readonly MouseClickManager _gridClickManager;  //声明,需要引入上面定义的类
this._gridClickManager = new MouseClickManager(this.dataGrid,300);//实例化,并指向需要控制的空间名称(this.DataGrid).放在构造函数中
this._gridClickManager.DoubleClick += new System.Windows.Input.MouseButtonEventHandler(_gridClickManager_DoubleClick);//委托双击事件.放在构造函数中

 void _gridClickManager_DoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)  //双击事件的实现
 {
    //doSomething(); 
 }
 private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)        //给表中的每一行加载双击事件
{
     e.Row.MouseLeftButtonUp+=_gridClickManager.HandleClick; 
}

http://blog.csdn.net/bychentufeiyang/article/details/7066347

http://blog.csdn.net/haukwong/article/details/7077711

http://www.xuebuyuan.com/261369.html

posted @ 2015-10-13 10:41  lwn6  阅读(2104)  评论(0编辑  收藏  举报
什么是幸福?天天在做自己想做的事情,家人、同事、朋友、客户、网友都和和睦睦,身体健康、钱包鼓鼓、女朋友天天开心、生活无忧无虑就是最大的幸福