Alexandru
Alexandru

Reputation: 12902

How do I add a row to my list view?

I can't seem to get this to work. Anyone have any ideas why?

Here is the markup:

<ListView Width="210" Height="83" Margin="0 0 5 0" Name="FiltersListView">
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Header="Column" Width="Auto" DisplayMemberBinding="{Binding FilterColumn}"></GridViewColumn>
                                    <GridViewColumn Header="Rule" Width="Auto" DisplayMemberBinding="{Binding FilterRule}"></GridViewColumn>
                                    <GridViewColumn Header="String" Width="Auto" DisplayMemberBinding="{Binding FilterString}"></GridViewColumn>
                                </GridView>
                            </ListView.View>
                        </ListView>

Here is the window initializer:

public SelectionWindow()
    {
        InitializeComponent();
        rows = new List<Row>();
        // Create a new binding object and set the binding of this list view to it.
        Binding myBinding = new Binding();
        myBinding.Source = rows;
        FiltersListView.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
    }

Here is my rows object and class:

List<Row> rows;
public class Row
    {
        public string FilterColumn;
        public string FilterRule;
        public string FilterString;
    }

But when I click this button, I don't see it getting added to the list:

private void AddButtonClick(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Adding row.");
            rows.Add(new Row { FilterColumn = "1", FilterRule = "2", FilterString = "3" });
            Console.WriteLine("Row added.");
        }

Upvotes: 0

Views: 161

Answers (1)

Vlad
Vlad

Reputation: 35594

This is because List<int> doesn't implement INotifyCollectionChanged, so the control doesn't know that the list has actually been updated.

Try to make rows an ObservableColelction<Row>.


Edit: as the OP mentioned, the other problem (empty rows) was due to using fields, not properties, on Row.

Upvotes: 1

Related Questions