Exemple #1
0
        public LiveDesignTimeProperty(DesignTimeHost <T> host, DesignTimeProperty property, Expression <Func <TValue> > liveValueProperty)
            : base(host, property)
        {
            PropertyObserver.For(host)
            .Observe(liveValueProperty.GetMemberName(), (s, e) =>
            {
                this.host.RaisePropertyChanged(this);
            });

            this.liveValueProperty = liveValueProperty;
            this.liveValueDelegate = this.liveValueProperty.Compile();
        }
        public AutocompleteViewModel()
        {
            this.Data = new ObservableCollection <Person>();

            PropertyObserver.For(this)
            .Observe(v => v.Choosen, (v, s) =>
            {
                var value = v.Choosen;
            })
            .Observe(v => v.UserText, (v, s) =>
            {
                if (this.UserText.Length >= 3)
                {
                    this.Data.Clear();
                    var filtered = storage.Where(p => p.FullName.StartsWith(this.UserText, StringComparison.OrdinalIgnoreCase));
                    foreach (var item in filtered)
                    {
                        this.Data.Add(item);
                    }
                }
            });
        }
Exemple #3
0
        /// <summary>
        /// Monitors the specified properties.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <param name="command">The command.</param>
        /// <param name="source">The source.</param>
        /// <param name="properties">The properties to monitor.</param>
        /// <returns>The original command.</returns>
        public static IDelegateCommand Observe <TSource>(this IDelegateCommand command, TSource source, params Expression <Func <TSource, Object> >[] properties)
            where TSource : INotifyPropertyChanged
        {
            Ensure.That(properties).Named(() => properties).IsNotNull();

            if (properties.Any())
            {
                var observer = PropertyObserver.For(source);

                foreach (var prop in properties)
                {
                    observer.Observe(prop);
                }

                command.AddMonitor(observer);
            }
            else
            {
                var observer = PropertyObserver.ForAllPropertiesOf(source);
                command.AddMonitor(observer);
            }

            return(command);
        }
Exemple #4
0
        public virtual IDelegateCommand CreateCommand(CommandData commandData)
        {
            var text = (commandData.Description == null) ?
                       String.Empty :
                       commandData.Description.DisplayText;

            var command = ( DelegateCommand )DelegateCommand.Create(text);

            command.SetData(commandData);

            command.OnCanExecute(o =>
            {
                var data = command.GetData <CommandData>();
                if (data.Fact != null)
                {
                    return(data.Fact.Eval(o));
                }
                else if (data.BooleanFact != null)
                {
                    var can = data.BooleanFact.FastGetter();
                    return(can);
                }

                return(true);
            })
            .OnExecute(o =>
            {
                var data = command.GetData <CommandData>();
                if (data.HasParameter)
                {
                    var prm = o;
                    if (o is IConvertible)
                    {
                        prm = Convert.ChangeType(o, data.ParameterType);
                    }

                    data.FastDelegate(data.DataContext, new[] { prm });
                }
                else
                {
                    data.FastDelegate(data.DataContext, null);
                }
            });

            if (commandData.KeyBindings != null)
            {
                commandData.KeyBindings
                .ForEach(kb => command.AddKeyGesture(kb.Key, kb.Modifiers));
            }

            IMonitor monitor = null;

            if (commandData.Fact != null)
            {
                monitor = commandData.Fact;
            }
            else if (commandData.BooleanFact != null)
            {
                monitor = PropertyObserver.For(( INotifyPropertyChanged )commandData.DataContext)
                          .Observe(commandData.BooleanFact.Name);
            }

            if (command != null && monitor != null)
            {
                command.AddMonitor(monitor);
                commandData.Monitor = monitor;
            }

            return(command);
        }
Exemple #5
0
        public ComplexGraphViewModel()
        {
            var observer = MementoObserver.Monitor(this.service);

            this.UndoCommand = DelegateCommand.Create()
                               .OnCanExecute(o => this.service.CanUndo)
                               .OnExecute(o => this.service.Undo())
                               .AddMonitor(observer);

            this.RedoCommand = DelegateCommand.Create()
                               .OnCanExecute(o => this.service.CanRedo)
                               .OnExecute(o => this.service.Redo())
                               .AddMonitor(observer);

            this.CreateNewAddressCommand = DelegateCommand.Create()
                                           .OnExecute(o =>
            {
                var address          = this.Entity.Addresses.AddNew();
                this.SelectedAddress = address;
            });

            this.DeleteAddressCommand = DelegateCommand.Create()
                                        .OnCanExecute(o => this.SelectedAddress != null)
                                        .OnExecute(o =>
            {
                this.SelectedAddress.Delete();
                this.SelectedAddress = this.Entity.Addresses.FirstOrDefault();
            })
                                        .AddMonitor
                                        (
                PropertyObserver.For(this)
                .Observe(v => v.SelectedAddress)
                                        );

            var person = new Person()
            {
                FirstName = "Mauro",
                LastName  = "Servienti"
            };

            person.Addresses.Add(new Address(person)
            {
                City    = "Treviglio",
                Number  = "11",
                Street  = "Where I live",
                ZipCode = "20100"
            });

            person.Addresses.Add(new Address(person)
            {
                City    = "Daolasa",
                Number  = "2",
                Street  = "Pierino",
                ZipCode = "20100"
            });

            var entity = new PersonViewModel();

            entity.Initialize(person, false);

            this.service.Attach(entity);

            this.Entity = entity;
        }