silverlight datagrid 模拟单元格双击事件

10 篇文章 0 订阅
4 篇文章 0 订阅

最近做一silverlight项目,需要在双击datagrid单元格的时候更具单元格内容弹出新窗口。

查了不少资料发现silverlight很难实现这个效果,于是就用了如下方法来模拟(用户体验和双击一样)。


         模拟双击事件
        private TimeSpan timeSpan = TimeSpan.Zero;
        private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp -= new MouseButtonEventHandler(Row_MouseLeftButtonUp);
            e.Row.MouseLeftButtonUp += new MouseButtonEventHandler(Row_MouseLeftButtonUp);
        }

        private void Row_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DateTime.Now.TimeOfDay - timeSpan < new TimeSpan(0, 0, 0, 0, 600))
            {
                timeSpan = new TimeSpan();

                //debug
                int columnIndex = dataGrid1.CurrentColumn.DisplayIndex;
                int rowIndex = dataGrid1.SelectedIndex;
                string currentValue = ((TextBlock)dataGrid1.CurrentColumn.GetCellContent(dataGrid1.SelectedItem)).Text;

                Message.InfoMessage(string.Format("row:{0},column:{1},value:{2}", rowIndex, columnIndex, currentValue));
            }

            timeSpan = DateTime.Now.TimeOfDay;
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGrid1.SelectedItem != null)
            {
                timeSpan = TimeSpan.Zero;

                //debug
                textBox1.DataContext = dataGrid1.CurrentColumn.Header.ToString() + ": " + ((TextBlock)dataGrid1.CurrentColumn.GetCellContent(dataGrid1.SelectedItem)).Text;
            }
        }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值