【问题标题】:How to add new items to DataGrid without overwriting the old ones?如何在不覆盖旧项目的情况下向 DataGrid 添加新项目?
【发布时间】:2014-04-19 13:30:20
【问题描述】:

我知道没有Append,但我相信这是正确的称呼方式(对吧?)。

现在我有一个ObservableCollection,我用它来向我的DataGrid 添加新项目。它工作得很好,但是如果我一次添加 10 个项目,第二次添加 15 个项目,我最终会得到 15 个项目,而不是 25 个。基本上,该集合会覆盖旧集合的每个集合。

可以循环遍历旧集合并在包含新集合后再次添加它,但这似乎是非常愚蠢的事情。我确信必须有另一种方法来做到这一点。

这是我的类里面的集合的样子 (I have used code from here):

public class ProxiesList
{

    public string proxy { get; set; }
    public string proxySource { get; set; }
    public bool proxyUsed { get; set; }
    public bool toUse { get; set; }
    public string working { get; set; }

    ObservableCollection<ProxiesList> GridItems = new ObservableCollection<ProxiesList>();

    public ObservableCollection<ProxiesList> _GridItems
    {
        get
        {
            return GridItems;
        }
    }

    public void addTheData(MainWindow mw, List<string> proxyList)
    {
        foreach (string line in proxyList)
        {
            _GridItems.Add(new ProxiesList //add items to the collection
            {
                proxy = line.Split(';')[0],
                proxySource = line.Split(';')[1],
                proxyUsed = false,
                toUse = true,
                working = "n/a"
            });
        }

        mw.dataGrid1.ItemsSource = GridItems; //bind the collection to the dataGrid1

        GridItems = new ObservableCollection<ProxiesList>(); //create new instance of the observable collection

    }

}

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    ObservableCollection 在添加、删除项目或刷新整个列表时提供通知。 因此,您只需将您的项目追加到您的数据网格绑定到的ObservableCollection&lt;ProxiesList&gt; 的同一实例中。

    所以基本上,您可以将mw.dataGrid1.ItemsSource 设置为ObservableCollection&lt;ProxiesList&gt; 或在Xaml 中使用Binding

    您无需在每次添加项目时都设置mw.dataGrid1.ItemsSource

    GridItems = new ObservableCollection<ProxiesList>(); //create new instance of the observable collection
    

    这一行导致了你的问题,因为它创建了一个新的集合实例,所以每次你将你的项目添加到ObservableCollection&lt;ProxiesList&gt;的一个新实例中,这意味着它只包含你刚刚添加的项目。

    所以只需从您的 public void addTheData(MainWindow mw, List&lt;string&gt; proxyList) 中删除以下行

        mw.dataGrid1.ItemsSource = GridItems; //bind the collection to the dataGrid1
    
        GridItems = new ObservableCollection<ProxiesList>(); //create new instance of the observable collection
    

    如果您将ProxiesList 设置为DataContext,您可以将Xaml 中的DataGrid.ItemsSource 绑定到您的_GridItems 属性。

    还有其他的东西,比如你的属性命名,最好遵循 C# 命名约定。

    【讨论】:

    • 尝试了一些事情 - 1) 我尝试删除第一行,但如果我添加到 DataGrid XAML - ItemsSource="{Binding GridItems}"ItemsSource="{Binding _GridItems}" 没有任何反应。 2) 仅删除第二行似乎不会改变我的初始结果(每次运行后覆盖) 3) 删除这两行会得到来自1) 的结果
    • 如果你只使用ItemsSource="{Binding GridItems}",你需要留意你的DataContext。您可以简单地将mw.dataGrid1.ItemsSource = GridItems; 设置为@har07 回答。
    • 我终于明白了。解决方案是在应用程序初始化中将ItemsSource 绑定到DataGrid。我已将您的答案标记为解决方案,因为您首先回答了。谢谢!
    • 谢谢,有几种方法可以绑定ItemSource。通常,我在 Xaml 中这样做。我应该向您提到您需要正确设置DataContext。绑定在MVVM中被广泛使用,我打赌你会想看看它。
    【解决方案2】:

    ObservableCollection 具有内置机制来通知 UI(在这种情况下是您的 DataGrid),每当项目添加到集合中或从集合中删除时。

    所以实例化ObservableCollection 并设置DataGridItemsSource 一次就足够了。例如,您可以在构造函数中执行此操作。剩下的就是简单地将新项目添加到现有集合中:

    public ProxiesList()
    {
        GridItems = new ObservableCollection<ProxiesList>();
        mw.dataGrid1.ItemsSource = GridItems;
    }
    
    public void addTheData(MainWindow mw, List<string> proxyList)
    {
        foreach (string line in proxyList)
        {
            GridItems.Add(new ProxiesList //add items to the collection
            {
                proxy = line.Split(';')[0],
                proxySource = line.Split(';')[1],
                proxyUsed = false,
                toUse = true,
                working = "n/a"
            });
        }
    }
    

    ...我看不出在此处保留_GridItems 的好处,因此您可以将其删除。

    【讨论】:

    • 现在它不会向DataGrid 添加任何内容,并且循环会在每个循环中绑定ItemsSource。出于测试目的,我将代码从循环中移出,没有任何改变——DataGrid 中没有添加任何内容,每次我尝试添加一个项目时,ProxyList() 都会被调用并重新绑定ItemsSource。还有其他方法(而不是使用构造函数)吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多