【问题标题】:TextBox inside a listview cell列表视图单元格内的文本框
【发布时间】:2013-10-10 19:14:48
【问题描述】:

我有一个列表视图,我想在每个 gridview 列单元格中添加一个文本框,以便我可以在其中输入数据,然后获取该数据。

我正在创建一个数据模板并将其传递给 GridViewColumn 的单元格模板,但是当我查看列表视图时,我无法向单元格添加任何内容。看起来甚至没有创建文本框。

            GridViewColumn conceptColumn = new GridViewColumn();
            conceptColumn.Header = conceptName;

            conceptColumn.CellTemplate = this.GetDataTemplate();
            this.TestModeler.Columns.Add(conceptColumn);
            conceptColumn.DisplayMemberBinding = new Binding(conceptName);


    private DataTemplate GetDataTemplate()
    {
        DataTemplate dt = new DataTemplate(typeof(TextBox));
        FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBox));
        dt.VisualTree = txtElement;

        Binding bind = new Binding();
        bind.Path = new PropertyPath("Text");
        bind.Mode = BindingMode.TwoWay;

        txtElement.SetBinding(TextBox.TextProperty, bind);
        txtElement.SetValue(TextBox.TextProperty, "test");

        return dt;
    }

【问题讨论】:

  • 不要在 WPF 的过程代码中创建或操作 UI 元素。这就是 XAML 的用途。

标签: wpf datatemplate celltemplate


【解决方案1】:

请查看 MSDN 上的 ListView Class 页面,您可以在其中找到 XAML 示例以及有关如何使用 WPF ListView 执行各种操作的大量链接。

您特别感兴趣,请查看How to: Use Templates to Style a ListView That Uses GridView 页面,该页面通过示例解释了您正在尝试做什么(但在 XAML 中)。

MSDN 应该始终是您首先查看的地方,因为它充满信息等待阅读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多