如何在列表视图中添加行?

时间:2013-05-28 19:52:47

标签: c# wpf xaml listview binding

我似乎无法让这个工作。任何人都有任何想法?

这是标记:

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

这是窗口初始化程序:

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

这是我的行对象和类:

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

但是当我点击此按钮时,我看不到它被添加到列表中:

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.");
        }

1 个答案:

答案 0 :(得分:1)

这是因为List<int>没有实现INotifyCollectionChanged,因此控件不知道该列表实际上已更新。

尝试将rows设为ObservableColelction<Row>


编辑:正如OP所提到的,另一个问题(空行)是由于在Row上使用字段而不是属性。