【问题标题】:How is a DataGrid getting updated when I add an item to it through the ICollectionViews SourceCollection object当我通过 ICollectionViews SourceCollection 对象向 DataGrid 添加项目时,如何更新 DataGrid
【发布时间】:2014-11-30 01:20:44
【问题描述】:

我对我的数据网格如何更新感到困惑。

我有一个如下定义的数据网格:

<DataGrid ItemsSource="{Binding Customers}" Style="{StaticResource DataGridDemoRowStyle}"     x:Name="grid"/>

c Customers 对象是在我的视图模型中找到的 ICollectionView。

在我的视图模型中,我像这样填充网格:

public ICollectionView Customers { get; private set; }

var _customers = new List<Customer>
                             {
                                 new Customer
                                     {
                                         FirstName = "Christian",
                                         LastName = "Moser",
                                         Gender = Gender.Male,
                                         WebSite = new Uri("http://www.wpftutorial.net"),
                                         ReceiveNewsletter = true,
                                         Image = "Images/christian.jpg"
                                     },
                                 new Customer
                                     {
                                         FirstName = "Peter",
                                         LastName = "Meyer",
                                         Gender = Gender.Male,
                                         WebSite = new Uri("http://www.petermeyer.com"),
                                         Image = "Images/peter.jpg"
                                     },
                                 new Customer
                                     {
                                         FirstName = "Lisa",
                                         LastName = "Simpson",
                                         Gender = Gender.Female,
                                         WebSite = new Uri("http://www.thesimpsons.com"),
                                         Image = "Images/lisa.jpg"
                                     },
                                 new Customer
                                     {
                                         FirstName = "Betty",
                                         LastName = "Bossy",
                                         Gender = Gender.Female,
                                         WebSite = new Uri("http://www.bettybossy.ch"),
                                         Image = "Images/betty.jpg"
                                     },
};

Customers = CollectionViewSource.GetDefaultView(_customers);

这按预期工作。然后我决定在网格不会更新的假设下尝试添加到集合中,但确实如此:

List<Customer> i = Customers.SourceCollection;
i.Add(new Customer() { FirstName = "Bobby" });

我的问题是,这是如何更新网格的?我没有在 ICollectionView 上调用 Refresh 方法。我只能假设 INotifyCollectionChanged 事件在某个时候被 ICollectionView 调用。

我的模型确实实现了 INotifyPropertyChanged,但我看不出这将如何更新网格以添加新项目,仅更新当前项目。

【问题讨论】:

    标签: c# wpf binding datagrid uicollectionview


    【解决方案1】:

    我已经像您一样测试了示例,但对其进行了一些修改,并且一切正常 - DataGrid 未更新。代码如下:

    public partial class MainWindow : Window
    {
        private ICollectionView customers;
        public MainWindow()
        {
            var _customers = new List<Customer>
            {
                // Fill collection
            };
    
            customers = CollectionViewSource.GetDefaultView(_customers);
            InitializeComponent();
            DataContext = customers;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var c = new Customer { FirstName = "The new", LastName = "The added" };
            // Note that cast is required, otherwise compilation error occurs.
            List<Customer> i = this.customers.SourceCollection as List<Customer>;
            i.Add(c);
        }
    }
    

    xaml:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <DataGrid ItemsSource="{Binding}" />
        <Button Content="Click me" Grid.Row="1" Click="Button_Click" />
    </Grid>
    

    目标.net框架是4.5,但我认为这不是那么重要。

    这意味着你错过了细节。在您的场景中,您可以为您的ICollectionView 实例的CollectionChanged 事件添加事件处理程序,在这里您可以了解这种行为的根本原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-10
      • 2011-11-19
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 2016-06-02
      相关资源
      最近更新 更多