Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中维护具有相同项的两个可观察集合。。?_C#_Wpf_Xaml_Mvvm_Observablecollection - Fatal编程技术网

C# 如何在WPF中维护具有相同项的两个可观察集合。。?

C# 如何在WPF中维护具有相同项的两个可观察集合。。?,c#,wpf,xaml,mvvm,observablecollection,C#,Wpf,Xaml,Mvvm,Observablecollection,我有一个可观察的集合,我在其中添加我的项目并在xaml中绑定“Listbox”。但我想维护这个可观察集合的克隆,并将该克隆可观察集合绑定到该“列表框”中,而不是原始可观察集合,然后首先将项目添加到该克隆可观察集合中,并通过单击按钮更新原始可观察集合。我正在使用MVVM Light来创建应用程序 有什么帮助吗。?这是我最初的可观察收藏 public ObservableCollection<ColumnNameDefinition> HistoricColumns { get; set

我有一个可观察的集合,我在其中添加我的项目并在xaml中绑定“Listbox”。但我想维护这个可观察集合的克隆,并将该克隆可观察集合绑定到该“列表框”中,而不是原始可观察集合,然后首先将项目添加到该克隆可观察集合中,并通过单击按钮更新原始可观察集合。我正在使用MVVM Light来创建应用程序

有什么帮助吗。?这是我最初的可观察收藏

public ObservableCollection<ColumnNameDefinition> HistoricColumns { get; set; }
public observateCollection HistoricColumns{get;set;}

我还没有测试过这段代码,但它应该可以正常工作。我已经在克隆上连接了CollectionChanged事件,并且我正在维护一个更改集合。然后,如果要提交更改,只需执行CommitChangesCommand,所有更改都将添加到源代码中

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Input;

namespace WpfApplication1
{
public class ViewModel
{
    private readonly ObservableCollection<Item> _clone;

    private readonly ObservableCollection<Item> _source;

    private readonly Collection<Item> _changes = new Collection<Item>();

    public ViewModel(ObservableCollection<Item> items)
    {
        _source = items;            
        _clone = new ObservableCollection<Item>(items);
        _clone.CollectionChanged += clone_CollectionChanged;
    }

    void clone_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                foreach (var newItem in e.NewItems.Cast<Item>())
                {
                    _changes.Add(newItem);
                }
                break;
        }
    }

    public ObservableCollection<Item> Clone
    {
        get { return _clone; }
    }

    private DelegateCommand _commitChangesCommand;

    public ICommand CommitChangesCommand
    {
        get { return _commitChangesCommand ?? (_commitChangesCommand = new DelegateCommand(CommitChanges, CanCommitChanges)); }
    }

    private void CommitChanges(object sender)
    {
        foreach (var change in _changes)
        {
            _source.Add(change);
        }

        _changes.Clear();
    }

    private bool CanCommitChanges(object sender)
    {
        return _changes.Any();
    }
}

public class Item
{
}

public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public event EventHandler CanExecuteChanged;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute,
        Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}
}
使用系统;
使用System.Collections.ObjectModel;
使用System.Collections.Specialized;
使用System.Linq;
使用System.Windows.Input;
命名空间WpfApplication1
{
公共类视图模型
{
私有只读可观察收集\u克隆;
私有只读可观察收集源;
私有只读集合_changes=new Collection();
公共视图模型(可观察的集合项)
{
_来源=项目;
_克隆=新的可观察集合(项目);
_clone.CollectionChanged+=clone\u CollectionChanged;
}
无效克隆\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
开关(电动)
{
案例NotifyCollectionChangedAction。添加:
foreach(e.NewItems.Cast()中的var newItem)
{
_更改。添加(新项);
}
打破
}
}
公共可观测收集克隆
{
获取{return\u clone;}
}
专用DelegateCommand _commitChangesCommand;
公共ICommand CommitChanges命令
{
获取{return _commitChangesCommand??(_commitChangesCommand=newdelegatecommand(CommitChanges,CanCommitChanges));}
}
私有无效提交更改(对象发送方)
{
foreach(变动中的var变动)
{
_来源。添加(更改);
}
_更改。清除();
}
私有布尔CanCommitChanges(对象发送方)
{
返回_changes.Any();
}
}
公共类项目
{
}
公共类DelegateCommand:ICommand
{
私有只读谓词_canExecute;
私有只读操作\u执行;
公共事件处理程序CanExecuteChanged;
公共DelegateCommand(操作执行)
:此(执行,空)
{
}
公共DelegateCommand(操作执行,
谓词(可执行)
{
_执行=执行;
_canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
返回_canExecute==null | | | u canExecute(参数);
}
public void Execute(对象参数)
{
_执行(参数);
}
public void raisecancecutechanged()
{
如果(CanExecuteChanged!=null)
{
CanExecuteChanged(此为EventArgs.Empty);
}
}
}
}

Use可以使用CopyTo方法感谢您的回复。但我想通过点击按钮,在克隆的可观察集合中只使用新添加的项来更新原始集合。我可以这样做吗?它将整个集合复制到一个兼容的一维数组中,从目标数组的指定索引开始,然后您可以在添加新项的位置记录数组索引…这样您就可以从中更新原始的ObservableCollectionSo,理解正确:您有一个包含一些数据的列表框。您可以按一个编辑按钮,在那里您可以绑定到克隆列表,在那里您可以添加/删除项目,最后按一个保存按钮,在那里您可以更改回原始数据,这些数据已经用克隆列表中的更改进行了更新?Naucy:是的。这正是我想要做的。