Search code examples
c#wpflistviewgridviewtextbox

How to add a column of textboxes to a ListView w/ GridView in C# - WPF dymanically?


I've got a ListView defined in XML:

    <ListView 
        x:Name="lvFuellmengenDetails"
        HorizontalAlignment="Center" 
        Height="570" 
        FontSize="30"
        Margin="10,85,10,0" 
        VerticalAlignment="Top" 
        HorizontalContentAlignment="Center"
        Width="572">

In the code behind I set the GridView as a view to the ListView to be able to add some columns

var gridView = new GridView();
        this.lvFuellmengenDetails.View = gridView;
        gridView.Columns.Add(new GridViewColumn
        {
            Header = "Nummer",
            Width = 200,
            DisplayMemberBinding = new Binding("Nummer")
        });
        gridView.Columns.Add(new GridViewColumn
        {
            Header = "Wert",
            Width = 200,

            DisplayMemberBinding = new Binding("Wert")
        });

I need another Column that should be editable in every line (e.g. Textboxes) - defined not in XML, but in C# code)

If that is not possible: is there a way to detect a certain line is clicked? I looked for an Event like "ItemClicked" but couldnt find any.

Then I could open a DialogBox and change the label instead of using textboxes. Thanks for your help.


Solution

  • To archive what you want, you have to either build the DataTemplate or get the resource and assign that onto the columns ItemTemplate

    it is more or less the same as if you would use normal XAML

    as the DataTemplate probably wont change, you probably want to create the DataTemplate in XAML and then just load it from the resources