环境:silverlight 4 ,vs2010, windows xp
xaml:
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);
对应事件 :
{
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 string Name { get; set; }
public int Age { get; set; }
}
添加数据:
List<DataGridSouceClass> ls = new List<DataGridSouceClass>();
DataGridSouceClass dc = new DataGridSouceClass();
dc.Age = 12;
dc.Name = "阿斌";
ls.Add(dc);
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>
/// Gets or sets a 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>
/// <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);
}
}
}
本文来自真的还可以吧的博客,原文地址:http://hi.baidu.com/%D5%E6%B5%C4%BB%B9%BF%C9%D2%D4%B0%C9/blog/item/141b912bbf13402d5343c1d6.html