彩票走势图

silverlight DataGrid 模拟实现双击行事件

转帖|其它|编辑:郝浩|2011-03-24 14:10:52.000|阅读 2194 次

概述:本文主要介绍silverlight DataGrid 模拟实现双击行事件,希望对大家有帮助。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

首先看一下效果:

  环境:silverlight 4 ,vs2010, windows xp

  xaml:

<sdk:DataGrid AutoGenerateColumns="False" Height="164" 

LoadingRow="ResultsGrid_LoadingRow"  
IsReadOnly="True" HorizontalAlignment="Left" Margin="69,70,0,0" 
Name="dataGrid1" VerticalAlignment="Top" Width="205">
             <sdk:DataGrid.Columns>
                 <sdk:DataGridTextColumn Header="名称" Width="80" Binding="{Binding Name}"/>
                 <sdk:DataGridTextColumn Header="年龄" Width="80" Binding="{Binding Age}"/>
             </sdk:DataGrid.Columns>
         </sdk:DataGrid>

  cs:

  定义一个变量 MouseClickManager 类在后面给出

private readonly MouseClickManager ;_gridClickManager;

  在构造函数中添加响应事件

 _gridClickManager = new MouseClickManager(dataGrid1, 300);
  ;     _gridClickManager.DoubleClick +=

 new System.Windows.Input.MouseButtonEventHandler

(_gridClickManager_DoubleClick);

  对应事件 :

  private void _gridClickManager_DoubleClick(object sender, 

MouseButtonEventArgs e)
         {
             DataGridSouceClass sc = (DataGridSouceClass)dataGrid1.SelectedItem;
             MessageBox.Show( "姓名:"+sc.Name+",年龄:"+sc.Age);
         }
         private void ResultsGrid_LoadingRow(object sender, DataGridRowEventArgs e)
         {
             e.Row.MouseLeftButtonUp += _gridClickManager.HandleClick;
        ; }  

  给DataGrid绑定数据:

  定义一个类:

     public class DataGridSouceClass
           {
                 public string Name { get; set; }
                 public int Age { get; set; }
 &nbsp;         }

  添加数据:

  List<DataGridSouceClass> ls = new List<DataGridSouceClass>();
             DataGridSouceClass dc = new DataGridSouceClass();
             dc.Age = 12;
             dc.Name =  "阿斌";
             ls.Add(dc);
 &nbsp;           dataGrid1.ItemsSource = ls;

  运行基本ok !

  MouseClickManager 类,是网上的人写的。具体是那点看到的,我不记得地址了,实在抱歉。

using System;
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 Elite.Silverlight
{
     public class MouseClickManager
     {
         public event MouseButtonEventHandler Click;
         public event MouseButtonEventHandler DoubleClick;

         ///  <summary>
  ///&nbsp;Gets or sets a&nbsp;value indicating whether this 

<see cref="MouseClickManager"/> is clicked.
         ///  </summary>
         ///  <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>
         private bool Clicked { get; set; }
 
         ///  <summary>
         /// Gets or sets the control.
         ///  </summary>
         ///  <value>The control.</value>
         public Control Control { get; set; }

         ///  <summary>
         /// Gets or sets the timeout.
         ///  </summary>
         ///  <value>The timeout.</value>
         public int Timeout { get; set; }

         ///  <summary>
  /// Initializes a new instance of the  <see cref="MouseClickManager"/> class.
         ///  </summary>
         ///  <param name="control">The control.</param>
         public MouseClickManager(Control control, int timeout)
         {
             this.Clicked = false;
             this.Control = control;
             this.Timeout = timeout;
         }

         ///  <summary>
         /// Handles the click.
         ///  </summary>
         ///  <param name="sender">The sender.</param>
       &nbsp; ///  <param name="e">The <see cref=

"System.Windows.Input.MouseButtonEventArgs"/>

 instance containing the event data.</param>
         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);
                 }
             }
         }

         ///  <summary>
         /// Resets the thread.
         ///  </summary>
         ///  <param name="state">The state.</param>
         private void ResetThread(object state)
         {
             Thread.Sleep(this.Timeout);

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

         ///  <summary>
         /// Called when [click].
         ///  </summary>
         ///  <param name="sender">The sender.</param>
         ///  <param name="e">The <see cref=

"System.Windows.Input.MouseButtonEventArgs"/>

 instance containing the event data.</param>
         private void OnClick(object sender, MouseButtonEventArgs e)
         {
             MouseButtonEventHandler handler = Click;

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

         ///  <summary>
         /// Called when [double click].
         ///  </summary>
         ///  <param name="sender">The sender.</param>
         ///  <param name="e">The <see cref=

"System.Windows.Input.MouseButtonEventArgs"/> 

instance containing the event data.</param>
         private void OnDoubleClick(object sender, MouseButtonEventArgs e)
         {
             MouseButtonEventHandler handler = DoubleClick;

             if (handler != null)
                 handler(sender, e);
         }
     }
}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP