重写DataGridTextColumn

编程入门 行业动态 更新时间:2024-10-23 04:46:18
本文介绍了重写DataGridTextColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图提供一个行为类似于DataGridTextColumn的DataGrid列,但在编辑模式下具有一个附加按钮。我看了看DataGridTemplateColumn,但是将DataGridTextColumn子类化如下似乎更容易

I am trying to provide a DataGrid column that behaves like the DataGridTextColumn, but with an additional button in editing mode. I looked at DataGridTemplateColumn, but it appears easier to subclass the DataGridTextColumn as below

问题是textBox添加到网格时会失去其绑定。也就是说,对其Text属性的更改不会反映在非编辑TextBlock或基础视图模式中。

The problem is the textBox loses its binding when added to the grid. That is, changes to its Text property are not reflected in the non-editing TextBlock or the underlying view-mode

关于这可能是为什么以及我如何工作的任何想法

Any thoughts on why this might be and how I can work around it?

public class DataGridFileColumn : DataGridTextColumn { protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { TextBox textBox = (TextBox)base.GenerateEditingElement(cell, dataItem); Button button = new Button { Content = "..." }; Grid.SetColumn(button, 1); return new Grid { ColumnDefinitions = { new ColumnDefinition(), new ColumnDefinition { Width = GridLength.Auto }, }, Children = { textBox, button, }, }; } }

我正在使用.NET 3.5和WPF工具包

I'm using .NET 3.5 and the WPF toolkit

推荐答案

事实证明,您还需要覆盖PrepareCellForEdit,CommitCellEdit和CancelCellEdit

It turns out you also need to override PrepareCellForEdit, CommitCellEdit and CancelCellEdit

基类(并非不合理地)假定传入的FrameworkElement将是TextBox

The base class assumes (not unreasonably) that the FrameworkElement passed in will be a TextBox

更多推荐

重写DataGridTextColumn

本文发布于:2023-11-03 12:43:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   DataGridTextColumn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!