/// <summary>
        /// Creates an <see cref="ObservableValue{TValue}"/> available as a property via data binding,
        /// and affected by a command accepting a <typeparamref name="TParam"/> parameter also available
        /// via data binding.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <typeparam name="TParam"></typeparam>
        /// <param name="viewModel"></param>
        /// <param name="propertyName"></param>
        /// <param name="command1Name"></param>
        /// <param name="inputObservableSelector"></param>
        /// <param name="initialValue"></param>
        /// <returns></returns>
        public static ObservableValue <TValue> CreateActiveProperty <TValue, TParam>(
            this BaseViewModel viewModel,
            string propertyName,
            string command1Name,
            Func <IObservable <TParam>, IObservable <TValue> > inputObservableSelector,
            TValue initialValue = default)
        {
            var command1 = viewModel.CreateObservableCommand <TParam>(command1Name);

            return(viewModel.CreateProperty(
                       propertyName,
                       () => inputObservableSelector(command1),
                       initialValue));
        }
        /// <summary>
        /// Creates an <see cref="ObservableValue{TValue}"/> available as a property via data binding,
        /// and affected by a command accepting a <typeparamref name="TParam"/> parameter also
        /// available via data binding, with an observable that determines if it can be executed. You
        /// can optionally expose that observable via a "Can" + commandName property.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <typeparam name="TParam"></typeparam>
        /// <param name="viewModel"></param>
        /// <param name="propertyName"></param>
        /// <param name="commandName"></param>
        /// <param name="inputObservableSelector"></param>
        /// <param name="canExecuteObservable"></param>
        /// <param name="initialValue"></param>
        /// <param name="includeCanProperty"></param>
        /// <returns></returns>
        public static ObservableValue <TValue> CreateActiveProperty <TValue, TParam>(
            this BaseViewModel viewModel,
            string propertyName,
            string commandName,
            Func <IObservable <TParam>, IObservable <TValue> > inputObservableSelector,
            IObservable <bool> canExecuteObservable,
            TValue initialValue     = default,
            bool includeCanProperty = false)
        {
            var command = viewModel.CreateObservableCommand <TParam>(commandName, canExecuteObservable, includeCanProperty);

            return(viewModel.CreateProperty <TValue>(
                       propertyName,
                       () => inputObservableSelector(command),
                       initialValue));
        }
Пример #3
0
        /// <summary>
        /// Creates an <see cref="IObservableCommand{T}"/> available via data binding, with support for <see cref="System.Windows.Input.ICommand.CanExecute"/>
        /// via an <see cref="IObservable{Boolean}"/>. You can optionally expose that observable via a "Can" + name property as well.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewModel"></param>
        /// <param name="name"></param>
        /// <param name="canExecuteObservable"></param>
        /// <param name="includeCanProperty"></param>
        /// <returns></returns>
        public static IObservableCommand <T> CreateObservableCommand <T>(
            this BaseViewModel viewModel,
            string name,
            IObservable <bool> canExecuteObservable,
            bool includeCanProperty = false)
        {
            var observableValue = new ObservableValue <IObservableCommand <T> >(new ObservableCommand <T>(canExecuteObservable));

            viewModel.AddProperty <IObservableCommand <T> >(name, observableValue);

            if (includeCanProperty)
            {
                viewModel.CreateProperty($"Can{name}", () => canExecuteObservable, true);
            }

            return(observableValue.Latest);
        }
Пример #4
0
 /// <summary>
 /// Creates an <see cref="ObservableValue{ObservableCollection{T}}"/> available as a property via data binding, with side effects
 /// that can affect the collection's contents. All notifications are fired on the UI thread.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="viewModel"></param>
 /// <param name="propertyName"></param>
 /// <param name="observableActions"></param>
 /// <param name="initialContent"></param>
 /// <returns></returns>
 public static ObservableValue <ObservableCollection <T> > CreateCollectionProperty <T>(
     this BaseViewModel viewModel,
     string propertyName,
     Func <ObservableCollectionProxy <T>, IObservable <Unit> > observableActions,
     IEnumerable <T> initialContent = null)
 {
     return(viewModel.CreateProperty <ObservableCollection <T> >(
                propertyName,
                () => Observable
                .Return(new ObservableCollection <T>(initialContent ?? Enumerable.Empty <T>()))
                .SelectMany(collection =>
                            observableActions(new ObservableCollectionProxy <T>(
                                                  viewModel.Dispatcher,
                                                  collection))
                            .IgnoreElements()
                            .Select(_ => default(ObservableCollection <T>))
                            .StartWith(collection))));
 }
        /// <summary>
        /// Creates an <see cref="ObservableValue{TValue}"/> available as a property via data binding,
        /// and affected by four commands respectively accepting <typeparamref name="TParam1"/>,
        /// <typeparamref name="TParam2"/>, <typeparamref name="TParam3"/> and
        /// <typeparamref name="TParam4"/> parameters also available via data binding.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <typeparam name="TParam1"></typeparam>
        /// <typeparam name="TParam2"></typeparam>
        /// <typeparam name="TParam3"></typeparam>
        /// <typeparam name="TParam4"></typeparam>
        /// <param name="viewModel"></param>
        /// <param name="propertyName"></param>
        /// <param name="command1Name"></param>
        /// <param name="command2Name"></param>
        /// <param name="command3Name"></param>
        /// <param name="command4Name"></param>
        /// <param name="inputObservableSelector"></param>
        /// <param name="initialValue"></param>
        /// <returns></returns>
        public static ObservableValue <TValue> CreateActiveProperty <TValue, TParam1, TParam2, TParam3, TParam4>(
            this BaseViewModel viewModel,
            string propertyName,
            string command1Name,
            string command2Name,
            string command3Name,
            string command4Name,
            Func <IObservable <TParam1>, IObservable <TParam2>, IObservable <TParam3>, IObservable <TParam4>, IObservable <TValue> > inputObservableSelector,
            TValue initialValue = default)
        {
            var command1 = viewModel.CreateObservableCommand <TParam1>(command1Name);
            var command2 = viewModel.CreateObservableCommand <TParam2>(command2Name);
            var command3 = viewModel.CreateObservableCommand <TParam3>(command3Name);
            var command4 = viewModel.CreateObservableCommand <TParam4>(command4Name);

            return(viewModel.CreateProperty(
                       propertyName,
                       () => inputObservableSelector(command1, command2, command3, command4),
                       initialValue));
        }