DataGrid双击选中单元格该为单击
2012-09-27 17:10
337 查看
By default, end users must double-click a cell in the DataGrid to enter edit mode. In certain cases, it may be more desirable for cells to enter edit mode with a single mouse click (for example, when editing a CheckBox). Use the code snippets below to implement
single-click editing in your DataGrid.
To apply single-click editing to all cells in the DataGrid
1. Paste the style below into the Resources of your DataGrid
2. Paste the method into the code behind
To apply single-click editing to only certain cells in the DataGrid
1. Set an x:Key on the Style (ex. <Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">)
2. Paste the style into the Resources of your DataGrid
3. Apply the style to the CellStyle property of the columns which you'd like to have single click editing (ex. <dg:DataGridCheckBoxColumn CellStyle="{StaticResource SingleClickEditing}"/>)
4. Paste the method into the code behind
single-click editing in your DataGrid.
To apply single-click editing to all cells in the DataGrid
1. Paste the style below into the Resources of your DataGrid
2. Paste the method into the code behind
To apply single-click editing to only certain cells in the DataGrid
1. Set an x:Key on the Style (ex. <Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">)
2. Paste the style into the Resources of your DataGrid
3. Apply the style to the CellStyle property of the columns which you'd like to have single click editing (ex. <dg:DataGridCheckBoxColumn CellStyle="{StaticResource SingleClickEditing}"/>)
4. Paste the method into the code behind
<!-- SINGLE CLICK EDITING --> <Style TargetType="{x:Type dg:DataGridCell}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter> </Style> // // SINGLE CLICK EDITING // private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid dataGrid = FindVisualParent<DataGrid>(cell); if (dataGrid != null) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) cell.IsSelected = true; } else { DataGridRow row = FindVisualParent<DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } } static T FindVisualParent<T>(UIElement element) where T : UIElement { UIElement parent = element; while (parent != null) { T correctlyTyped = parent as T; if (correctlyTyped != null) { return correctlyTyped; } parent = VisualTreeHelper.GetParent(parent) as UIElement; } return null; }
相关文章推荐
- Wpf Datagrid 设置了模板 checkbox 如何一次选中,单击就选中。默认是要双击才得
- 【原创】有关Silverlight中“DataGrid中单元格动态绑定ComboBox单击时数据项莫名被清除 ”的解决方案及思路。
- 方便的使用单击和双击更新DataGrid中的数据的例子
- SelectHelper 支持单击选中 双击选中 多选 连续多选
- DataGrid中添加单击或者双击事件
- 完成功能是DataGrid的页面选中事件、鼠标事件、双击事件改变TR的色彩
- 让你的GridView、DataList 的数据行同时具有单击或双击选中的功能
- WPF DataGrid 鼠标双击 选中的 DataGridRow 及 行对应数据
- 鼠标单击TabSequence=0的单元格时黑色选中色变为其他颜色
- DataGridView的具体应用(1)-右键单击后选中所在行及弹出上下文菜单,通过事件捕获所选行的ID号及单元格内容
- 使用单击和双击更新DataGrid中的数据的例子
- easyui datagrid 单击行不选中
- easyui datagrid组件 单击行 让他不选中 只有单击checkbox的时候才选中
- silverlight datagrid 模拟单元格双击事件
- 如何让DataGrid选中一行,而不是单元格
- ASP.NET GridView单击选中行,双击打开详细页面(網上轉載)
- javascript 单选按钮 单击选中 双击取消选择实现代码
- DataGrid单元格移动变色,点击变色,双击取消颜色,排序.
- DataGrid单元格移动变色,点击变色,双击取消颜色,排序.