internal SourceItemCollections( IViewModel selectionVM, Func <TItemSource, bool> isActiveFilter ) { IEnumerable <TItemSource> allItems = SelectionHelpers .GetAllSourceItems <TItemSource>(selectionVM); IEnumerable <TItemSource> selectedItems = SelectionHelpers .GetSelectedSourceItems <TItemSource>(selectionVM); AllSourceItems = new HashSet <TItemSource>(allItems); IEnumerable <TItemSource> selectableItems = allItems; if (isActiveFilter != null) { selectableItems = selectableItems.Where(isActiveFilter); } if (selectedItems != null && selectedItems.Any()) { // Add items that are either inactive but currently selected or do // are not contained in the all items at all. selectableItems = selectableItems.Union(selectedItems); } SelectableItems = selectableItems.ToList(); }
public SelectableItemsCollection <TItemSource> GetSelectableItems(IBehaviorContext context) { SelectableItemsCollection <TItemSource> items; if (!context.FieldValues.TryGetValue(_items, out items)) { var source = ((IHasSourceObject <TSourceObject>)context.VM).Source; Func <TItemSource, bool> reducedFilter = null; if (IsActiveFilter != null) { reducedFilter = item => IsActiveFilter(source, item); } items = new SelectableItemsCollection <TItemSource>( SelectionHelpers.GetAllSourceItems <TItemSource>(context.VM), SelectionHelpers.GetSelectedSourceItems <TItemSource>(context.VM), reducedFilter ); context.FieldValues.SetValue(_items, items); } return(items); }
public static void OnlyExistingItemsAreSelected <TOwner, TTarget, TSource, TVM>( this ValidatorBuilder < TOwner, TTarget, SingleSelectionVMDescriptor <TSource, TVM> > builder, string errorMessage = null, object details = null ) where TTarget : IViewModel <SingleSelectionVMDescriptor <TSource, TVM> > where TOwner : IViewModel where TVM : IViewModel, IHasSourceObject <TSource> { errorMessage = errorMessage ?? Localized.SelectedItemsNotInSourceItems; builder.Check(x => x.SelectedItem).Custom(args => { var selectionVM = args.Target; if (args.Value != null) { if (!SelectionHelpers.IsItemContainedInAllSourceItems(selectionVM, args.Value.Source)) { args.AddError(errorMessage, details); } } }); }
public static void OnlyExistingItemsAreSelected <TOwner, TTarget, TSource, TVM>( this ValidatorBuilder < TOwner, TTarget, MultiSelectionVMDescriptor <TSource, TVM> > builder, string errorMessage = null, object details = null ) where TTarget : IViewModel <MultiSelectionVMDescriptor <TSource, TVM> > where TOwner : IViewModel where TVM : IViewModel, IHasSourceObject <TSource> { errorMessage = errorMessage ?? Localized.SelectedItemsNotInSourceItems; builder.CheckCollection(x => x.SelectedItems).Custom(args => { var selectionVM = args.Items.OwnerVM; foreach (TVM selectedItem in args.Items) { if (!SelectionHelpers.IsItemContainedInAllSourceItems(selectionVM, selectedItem.Source)) { args.AddError(selectedItem, errorMessage, details); } } }); }
public SingleSelectionDescriptorBuilderBase( IVMDescriptor itemDescriptor, Func <TSourceObject, TItemSource, bool> isActiveFilter ) { WithProperties((d, b) => { var v = b.GetPropertyBuilder(); d.SelectedItem = v.VM.DelegatesTo( (vm) => vm.GetSelectedItem(), (vm, val) => vm.SetSelectedItem(val) ); d.AllItems = v .Collection .PopulatedWith(vm => { IEnumerable <TItemSource> selectableItems = SelectionHelpers .GetSelectableSourceItems <TSourceObject, TItemSource>(vm); return(selectableItems.Select(x => vm.GetItemVM(x))); }) .With(itemDescriptor); }); WithViewModelBehaviors(b => { b.OverrideUpdateFromSourceProperties( x => x.AllSourceItems, x => x.SelectedSourceItem, x => x.AllItems, x => x.SelectedItem ); b.OverrideUpdateSourceProperties( x => x.SelectedSourceItem ); b.AppendBehavior( new SelectionItemViewModelCacheBehavior <TItemSource, TItemVM>(itemDescriptor) ); b.PrependBehavior(new ItemProviderBehavior <TSourceObject, TItemSource>() { IsActiveFilter = isActiveFilter }); }); WithBehaviors(b => { b.Property(x => x.AllSourceItems).IsCached(); b.Property(x => x.SelectedItem).PrependBehavior(new SelectedItemRefreshBehavior <TItemVM>()); // TODO: Make this configurable. b.Property(x => x.SelectedSourceItem).RequiresLoadedProperty(x => x.AllSourceItems); }); WithValidators(b => { b.EnableParentValidation(x => x.SelectedItem); }); }
private void DiscardUnusedEntries(IBehaviorContext context) { var activeSourceItems = new HashSet <TItemSource>( SelectionHelpers.GetAllSourceItems <TItemSource>(context.VM) ); Dictionary <TItemSource, TItemVM> cache = GetCache(context); foreach (TItemSource entrySource in cache.Keys.ToArray()) { if (!activeSourceItems.Contains(entrySource)) { cache.Remove(entrySource); } } }
public MultiSelectionDescriptorBuilderBase( IVMDescriptor itemDescriptor, Func <TSourceObject, TItemSource, bool> isActiveFilter ) { WithProperties((d, b) => { var v = b.GetPropertyBuilder(); d.SelectedItems = v .Collection .Wraps(x => x.SelectedSourceItems) .With <TItemVM>(itemDescriptor); d.AllItems = v .Collection .PopulatedWith(vm => { IEnumerable <TItemSource> selectableItems = SelectionHelpers .GetSelectableSourceItems <TSourceObject, TItemSource>(vm); return(selectableItems.Select(x => vm.GetItemVM(x))); }) .With(itemDescriptor); }); WithViewModelBehaviors(b => { b.OverrideUpdateFromSourceProperties( x => x.AllSourceItems, x => x.SelectedSourceItems, x => x.AllItems, x => x.SelectedItems ); b.OverrideUpdateSourceProperties( x => x.SelectedSourceItems ); b.AppendBehavior( new SelectionItemViewModelCacheBehavior <TItemSource, TItemVM>(itemDescriptor) ); b.AppendBehavior(new ItemProviderBehavior <TSourceObject, TItemSource>() { IsActiveFilter = isActiveFilter }); }); WithBehaviors(b => { b.Property(x => x.SelectedItems).Enable( PropertyBehaviorKeys.ValueAccessor, new SelectedItemsAccessorBehavior <TVM, TItemVM, TItemSource>() ); // This behavior allows a bound comobox to assign a new list to the 'SelectedItems' // property every time the selection changes. b.Property(x => x.SelectedItems).Enable( PropertyBehaviorKeys.DisplayValueAccessor, new SettableListDisplayValueBehavior <TItemVM>() ); b.Property(x => x.SelectedItems).AddChangeHandler((vm, args) => { vm.RaisePropertyChangedForSelectedItems(); // HACK! }); b.Property(x => x.AllSourceItems).IsCached(); b.Property(x => x.SelectedItems).SupportsDisplayValueConversion(); b.Property(x => x.SelectedItems).PrependBehavior(new SelectedItemsRefreshBehavior <TItemVM>()); // TODO: Make this configurable. b.Property(x => x.SelectedSourceItems).RequiresLoadedProperty(x => x.AllSourceItems); }); WithValidators(b => { b.EnableParentValidation(x => x.SelectedItems); b.EnableParentViewModelValidation(); }); }