重写 DataGridTextColumn

发布于 2024-12-02 08:40:13 字数 928 浏览 1 评论 0原文

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

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

关于为什么会出现这种情况以及如何解决它有什么想法吗?

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 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

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,
            },
        };
    }
}

I'm using .NET 3.5 and the WPF toolkit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2024-12-09 08:40:13

事实证明,您还需要重写PrepareCellForEdit、CommitCellEdit 和CancelCellEdit

基类假设(并非没有道理)传入的FrameworkElement 将是一个TextBox

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

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

桃气十足 2024-12-09 08:40:13

我认为您必须在GenerateEditingElement(...) 方法中手动设置绑定。

一旦您从基类中获取了 TextBox,请像这样设置其绑定:

textBox.DataContext = dataItem;
textBox.SetBinding(TextBlock.TextProperty, Binding);

无论如何,这对我有用。

请注意,我不确定为什么这有效,因为阅读GenerateEditingCell 对我来说意味着您从基类获取的 TextBox 应该已经正确设置了绑定。然而,上述方法正是他们在这篇博文中所做的< /a>.

编辑:

您实际上不需要设置绑定,它已经完成(正如文档中所述)。不过,您确实需要设置 DataContext,因为由于某种原因,它没有在从基类返回的文本框中设置。

I think you have to set up the binding manually in the GenerateEditingElement(...) method.

Once you've grabbed the TextBox from the base class, set up its binding like this:

textBox.DataContext = dataItem;
textBox.SetBinding(TextBlock.TextProperty, Binding);

This works for me anyway.

Note, I'm not sure why this works, as reading the documentation for GenerateEditingCell implies to me that the TextBox that you grab from the base class should already have its bindings set up properly. However, the above approach is what they did in this blog post.

EDIT:

You don't actually need to set up the binding, it is done already (as it says in the docs). You do need to set up the DataContext though, as for some reason this isn't set up on the textBox returned from the base class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文