silverlight datagrid 模拟单元格双击事件

最近做一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;
            }
        }


你可能感兴趣的:(object,datagrid,String,null,silverlight,textbox)