datagrid之类的组件中行内按钮点击和行点击事件问题

记录一下问题,防止忘记。高手请无视。

复制一张妹子做的图片,希望她看到之后不要打我


datagrid之类的组件中行内按钮点击和行点击事件同时发生的问题,

解决办法:

event.stopPropagation();

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
datagrid 点击单元格事件编辑的具体实现可以参考以下步骤: 1. 在 DataGrid 控件中设置 editable 属性为 true,以启用编辑模式。 2. 绑定 DataGrid 的 ItemClick 事件,用于捕获单元格的点击事件。 3. 在 ItemClick 事件中,通过获取当前点击的单元格的号和列号,进而获取该单元格对应的数据项。 4. 创建一个编辑器(如 TextBox、ComboBox 等),并将其添加到当前单元格中,以供用户编辑。 5. 在编辑器中,绑定 TextChanged、SelectionChanged 等事件,用于在用户编辑完成后更新数据项的值。 6. 在编辑完成后,通过 DataGrid 的 ItemsSource 属性更新数据源,以更新显示的数据。 下面是一个示例代码: ```C# private void DataGrid_ItemClick(object sender, ItemClickEventArgs e) { // 获取当前点击的单元格的号和列号 int row = DataGrid.ItemContainerGenerator.IndexFromContainer((DataGridRow)sender); int col = DataGrid.CurrentColumn.DisplayIndex; // 获取当前单元格对应的数据项 DataRowView rowView = (DataRowView)e.Item; string value = rowView.Row.ItemArray[col].ToString(); // 创建编辑器并添加到当前单元格中 TextBox editor = new TextBox(); editor.Text = value; editor.TextChanged += (s, args) => { // 在编辑器中,绑定 TextChanged 事件,用于在用户编辑完成后更新数据项的值 rowView.Row.ItemArray[col] = editor.Text; }; // 将编辑器添加到当前单元格中,并设置焦点 DataGridCell cell = GetCell(DataGrid, row, col); cell.Content = editor; editor.Focus(); } private DataGridCell GetCell(DataGrid dataGrid, int row, int column) { DataGridRow rowContainer = dataGrid.GetRow(row); if (rowContainer != null) { DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); if (presenter != null) { DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); if (cell != null) return cell; } } return null; } private T GetVisualChild<T>(Visual parent) where T : Visual { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual visual = (Visual)VisualTreeHelper.GetChild(parent, i); child = visual as T ?? GetVisualChild<T>(visual); if (child != null) break; } return child; } ``` 以上代码仅供参考,具体实现还需要根据具体情况进调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值