[英]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. GridData
和SelectedEntities
绑定到数据网格,并且像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
属性,我想在IsSingeselect
或IsMultiSelect
属性上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.