WPF ViewModel问题

时间:2014-08-28 19:43:49

标签: c# wpf

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

视图模型

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;


    }

1 个答案:

答案 0 :(得分:1)

问题是你打破了原始馆藏的链接。

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

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

每次设置itemsnull时,都会将Entries设置为SubGroups以重置Entries,以便下次重新生成SubGroups,同时考虑新CompositeCollection 1}}和new CompositeCollection { new CollectionContainer { Collection = this.Entries }, new CollectionContainer { Collection = this.SubGroups } };

2)尝试使用{{1}}:

{{1}}

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