简体   繁体   English

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

[英]How to implement chained/proxied databindings

I have this problem in my mvvm wpf application where I want to create proxied/chained databindings. 我的mvvm wpf应用程序中存在此问题,我想在其中创建代理/链接的数据绑定。

My ModelViewController looks like this: 我的ModelViewController看起来像这样:

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 and SelectedEntities is bound to a datagrid and works like a charm. GridDataSelectedEntities绑定到数据网格,并且像GridData按钮一样工作。 I'm using the ContextMenuItems collection to create BarButtonItems for the datagrids contextmenu, this works very good. 我正在使用ContextMenuItems集合为datagrids contextmenu创建BarButtonItems ,这很好用。 The MenuComandModel class has a Enabled attribute and I want to bind this on the IsSingeselect or IsMultiSelect attribute to the BarButtonItems member IsEnabled . MenuComandModel类具有Enabled属性,我想在IsSingeselectIsMultiSelect属性上IsMultiSelect属性绑定到BarButtonItems成员IsEnabled How would I archive this? 我将如何存档?

Since you are using DevExpress you can use all the benefits of DevExpress MVVM Framework and their POCO-ViewModels : 由于您使用的是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
        }
    }
}

Then you can bind you bar items to the ContextMenuItems collection using the approach described in MVVM Support in DXBars, DXRibbon and GalleryControl help-article. 然后,您可以使用DXBars,DXRibbon和GalleryControl帮助文章中的MVVM支持中介绍的方法将条形项目绑定到ContextMenuItems集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM