我试图提供一个行为类似于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
发布评论