0

I have a listview that I would like to add a textbox inside each gridview column cell so I can type data into it and then fetch that data.

I'm creating a datatemplate and passing it to a cell template for the GridViewColumn but when I look at the listview I can't add anything to the cell. It doesn't look like the textbox was even created.

            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;
    }
chuckd
  • 13,460
  • 29
  • 152
  • 331

1 Answers1

0

Please take a look at the ListView Class page at MSDN where you can find a XAML example and plenty of link on how to do various things with a WPF ListView.

Of particular interest to you, please take a look at the How to: Use Templates to Style a ListView That Uses GridView page there which explains what you are trying to do (but in XAML) with examples.

MSDN should always be your first place to look as it is full of information just waiting to be read.

Sheridan
  • 68,826
  • 24
  • 143
  • 183