2016-03-14 48 views
0

我在我想創建代理/鏈接數據綁定的mvvm wpf應用程序中遇到此問題。如何實現鏈接/代理數據綁定

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

GridDataSelectedEntities綁定到一個DataGrid和工程就像一個魅力。我正在使用ContextMenuItems集合爲數據網格的上下文菜單創建BarButtonItems,這非常好。 MenuComandModel類具有Enabled屬性,我想將此屬性與IsSingeselectIsMultiSelect屬性綁定到BarButtonItems成員IsEnabled。我將如何歸檔這個?

回答

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集合。

+0

感謝您的靈感,這給了我需要的輸入。由於我沒有時間,我沒有將視角模型轉換爲poco。但最後我有一個解決方案。非常感謝! –

+0

我很高興聽到我的幫助是有幫助的! – DmitryG

相關問題