WPF 视图模型问题

本文关键字:问题 模型 视图 WPF | 更新日期: 2023-09-27 18:19:48

我有一个用于树视图的递归视图模型。问题是每次我删除组项目或条目项目时。树视图无法立即反映更改,直到我通过树视图强制它。Items.Refresh((。我的猜测是可观察集合项目确实得到了通知。你能指出我吗?谢谢。

视图模型

public class Group:ViewModelBase
{
    private int _key;
    public int Key
    {
        get { return _key; }
        set { _key = value; OnPropertyChanged("Key"); }
    }
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }
    private bool _isexpanded;
    public bool IsExpanded
    {
        get { return _isexpanded; }
        set { _isexpanded = value; OnPropertyChanged("IsExpanded"); }
    }
    private int _order;
    public int Order
    {
        get { return _order; }
        set { _order = value; OnPropertyChanged("Order"); }
    }
    private int _grouporder;
    public int GroupOrder
    {
        get { return _grouporder; }
        set { _grouporder = value; OnPropertyChanged("GroupOrder"); }
    }
    private string _error;
    public string Error
    {
        get { return _error; }
        set { _error = value; OnPropertyChanged("Error"); }
    }
    private ObservableCollection<Group> _subgroups;
    public ObservableCollection<Group> SubGroups
    {
        get { return _subgroups; }
        set { _subgroups = value; 
            OnPropertyChanged("SubGroups");
            OnPropertyChanged("Entries");
        }
    }
    private ObservableCollection<Entry> _entries;
    public ObservableCollection<Entry> Entries
    {
        get { return _entries; }
        set { _entries = value;
            OnPropertyChanged("SubGroups");
            OnPropertyChanged("Entries"); }
    }

    public ObservableCollection<object> Items
    {
        get
        {
            ObservableCollection<object> childNodes = new ObservableCollection<object>();
            foreach (var entry in this.Entries)
                childNodes.Add(entry);
            foreach (var group in this.SubGroups)
                childNodes.Add(group);
            return childNodes;
        }
    }
}

捆绑:

 public MainWindow()
    {
        InitializeComponent();
        treeview.ItemsSource = Groups;
        this.DataContext = this;

    }

WPF 视图模型问题

问题是您断开了与原始集合的链接。

所以我可以想象两个解决方案:

1( 注册EntriesSubGroups的更改并将其应用回Items

private ObservableCollection<object> items;
public ObservableCollection<object> Items
{
    get
    {
        if (items == null)
        {
            items = new ObservableCollection<object>();
            foreach (var entry in this.Entries)
                items.Add(entry);
            foreach (var group in this.SubGroups)
                items.Add(group);

            this.Entries.CollectionChanged += (s, a) =>
            {
                if (/*add some stuff*/)
                {
                    items.Add(/*some stuff*/)
                }
                else if (/*remove some stuff*/)
                {
                    items.Remove(...)
                }
            };
            this.SubGroups.CollectionChanged += ...
            return items;
        }
    }
}

您可以通过在每次设置EntriesSubGroups时将其设置为null来重置items,以便下次重新生成它,同时考虑到新的EntriesSubGroups

2(尝试使用CompositeCollection

new CompositeCollection
{
    new CollectionContainer { Collection = this.Entries },
    new CollectionContainer { Collection = this.SubGroups }
};

不确定第二个解决方案,但它会非常优雅,值得一试......