Esempio n. 1
0
        private ICommand CreateRoutingCommand()
        {
            if (Command == null)
            {
                return(null);
            }

            ContextAwareDelegateCommand <object> command = null;

            command = new ContextAwareDelegateCommand <object>(o => Command.Execute((TContext)command.Context),
                                                               o => Command.CanExecute((TContext)command.Context));
            Command.CanExecuteChanged += (sender, args) => command.RaiseCanExecuteChanged();

            return(command);
        }
Esempio n. 2
0
        private ICommand CreateRoutingCommand()
        {
            if (Command != null && MultipleCommand != null)
            {
                ContextAwareDelegateCommand <object> command = null;
                command = new ContextAwareDelegateCommand <object>(o =>
                {
                    if (o is TItem item)
                    {
                        Command.Execute(item, (TContext)command.Context);
                    }
                    else if (o is IList list)
                    {
                        if (list.Count == 1)
                        {
                            Command.Execute(list.Cast <TItem>().Single(), (TContext)command.Context);
                        }
                        else
                        {
                            MultipleCommand.Execute(list.Cast <TItem>().ToList(), (TContext)command.Context);
                        }
                    }
                }, o =>
                {
                    if (o is TItem item)
                    {
                        return(Command.CanExecute(item, (TContext)command.Context));
                    }
                    if (o is IList list)
                    {
                        if (list.Count == 1)
                        {
                            return(Command.CanExecute(list.Cast <TItem>().Single(), (TContext)command.Context));
                        }
                        else
                        {
                            return(MultipleCommand.CanExecute(list.Cast <TItem>().ToList(), (TContext)command.Context));
                        }
                    }

                    return(true);
                });

                Command.CanExecuteChanged         += (sender, args) => command.RaiseCanExecuteChanged();
                MultipleCommand.CanExecuteChanged += (sender, args) => command.RaiseCanExecuteChanged();
                return(command);
            }

            if (MultipleCommand != null)
            {
                ContextAwareDelegateCommand <IList> command = null;
                command = new ContextAwareDelegateCommand <IList>(
                    list => MultipleCommand.Execute(list.Cast <TItem>().ToList(), (TContext)command.Context),
                    list => MultipleCommand.CanExecute(list.Cast <TItem>().ToList(), (TContext)command.Context));
                MultipleCommand.CanExecuteChanged += (sender, args) => command.RaiseCanExecuteChanged();
                return(command);
            }
            else
            {
                ContextAwareDelegateCommand <object> command = null;
                command = new ContextAwareDelegateCommand <object>(
                    o => Command.Execute((TItem)o, (TContext)command.Context),
                    o => Command.CanExecute((TItem)o, (TContext)command.Context));
                return(command);
            }
        }