zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    mongodb简介
    tomcat简介
    date的详细说明
    30岁前成功的12条黄金法则
    2012春晚经典语录
    统计文件中某个单词出现的次数
    nginx简介
    ATM取款机系统模拟仿真
    十三种时间管理方法
    笔试常见的智力题 附答案
  • 原文地址:https://www.cnblogs.com/lwngreat/p/4873837.html
Copyright © 2011-2022 走看看