如何实现链式/代理数据绑定

时间:2016-03-14 08:03:48

标签: c# wpf mvvm data-binding devexpress

我在我的mvvm wpf应用程序中遇到此问题,我想在其中创建代理/链接数据绑定。

我的ModelViewController看起来像这样:

positions

public class ListViewModel { public ObservableCollection<Contact> GridData { get; private set; } public ObservableCollection<Contact> SelectedEntities { get; private set; } public bool IsSingeselect { get { return SelectedEntities.Count == 1; } } public bool IsMultiSelect { get { return SelectedEntities.Count > 0; } } public ObservableCollection<MenuComandModel> ContextMenuItems { get; private set; } } GridData绑定到数据网格,就像魅力一样。我正在使用SelectedEntities集合为datagrids contextmenu创建BarButtonItems,这非常有用。 ContextMenuItems类具有MenuComandModel属性,我希望将此EnabledIsSingeselect属性绑定到IsMultiSelect成员BarButtonItems。我该如何存档?

1 个答案:

答案 0 :(得分:1)

由于您使用的是DevExpress,因此可以使用DevExpress MVVM Framework及其POCO-ViewModels的所有好处:

using DevExpress.Mvvm.POCO;
//...
public class ListViewModel {
    public ObservableCollection<Contact> GridData { get; private set; }

    // mark the SelectedEntities property as virtual to be notified on initializing/replacing
    public virtual ObservableCollection<Contact> SelectedEntities { get; private set; }
    // unsubscribe the CollectionChanged event in changing-callback
    protected void OnSelectedEntitiesChanging() {
        if(SelectedEntities != null)
            SelectedEntities.CollectionChanged -= SelectedEntities_CollectionChanged;
    }
    // subscribe the CollectionChanged event in changed-callback
    protected void OnSelectedEntitiesChanged() {
        if(SelectedEntities != null)
            SelectedEntities.CollectionChanged += SelectedEntities_CollectionChanged;
        UpdateSelectedEntitiesDependencies();
    }
    void UpdateSelectedEntitiesDependencies() {
        // Raise INPC for dependent properties
        this.RaisePropertyChanged(x => x.IsSingeselect);
        this.RaisePropertyChanged(x => x.IsMultiSelect);
        // Raise INPC for dependent properties of child ViewModels
        foreach(var item in ContextMenuItems)
            item.RaisePropertyChanged(x => x.Enabled);
    }
    void SelectedEntities_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
        if(e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
            UpdateSelectedEntitiesDependencies();
    }
    public bool IsSingeselect { get { return SelectedEntities.Count == 1; } }
    public bool IsMultiSelect { get { return SelectedEntities.Count > 0; } }
    public ObservableCollection<MenuComandViewModel> ContextMenuItems { get; private set; }
}

public class MenuComandViewModel {
    public bool Enabled {
        get {
            var parentViewModel = this.GetParentViewModel<ListViewModel>();
            return parentViewModel.IsMultiSelect; // Some implementation
        }
    }
}

然后,您可以使用MVVM Support in DXBars, DXRibbon and GalleryControl帮助文章中描述的方法绑定条形项目到ContextMenuItems集合。