Esempio n. 1
0
        public void CanExecuteChangedTest()
        {
            var command = new RelayCommand(() =>
            {
            },
                                           () => true);

            var canExecuteChangedCalled = 0;

            var canExecuteChangedEventHandler = new EventHandler((s, e) => canExecuteChangedCalled++);

            command.CanExecuteChanged += canExecuteChangedEventHandler;

            command.RaiseCanExecuteChanged(true);

#if SILVERLIGHT
            Assert.AreEqual(1, canExecuteChangedCalled);
#else
            // In WPF, cannot trigger the CanExecuteChanged event like this
            Assert.AreEqual(0, canExecuteChangedCalled);
#endif

            command.CanExecuteChanged -= canExecuteChangedEventHandler;
            command.RaiseCanExecuteChanged(true);

#if SILVERLIGHT
            Assert.AreEqual(1, canExecuteChangedCalled);
#else
            // In WPF, cannot trigger the CanExecuteChanged event like this
            Assert.AreEqual(0, canExecuteChangedCalled);
#endif
        }
 public void RegisterPropertyChanged(RelayCommand command)
 {
     _target.PropertyChanged += (sender, args) =>
     {
         if (_dependentPropertyNames.Contains(args.PropertyName))
         {
             command.RaiseCanExecuteChanged();
         }
     };
 }
        public void TestCanExecuteChanged()
        {
            var command = new RelayCommand<string>(
                p =>
                {
                },
                p => true);

            var canExecuteChangedCalled = 0;

            var canExecuteChangedEventHandler = new EventHandler((s, e) => canExecuteChangedCalled++);

            command.CanExecuteChanged += canExecuteChangedEventHandler;

            command.RaiseCanExecuteChanged();

#if SILVERLIGHT
             Assert.AreEqual(1, canExecuteChangedCalled);
#elif NETFX_CORE
            Assert.AreEqual(1, canExecuteChangedCalled);
#elif PORTABLE
            Assert.AreEqual(1, canExecuteChangedCalled);
#else
            // In WPF, cannot trigger the CanExecuteChanged event like this
            Assert.AreEqual(0, canExecuteChangedCalled);
#endif

            command.CanExecuteChanged -= canExecuteChangedEventHandler;

            command.RaiseCanExecuteChanged();

#if SILVERLIGHT
             Assert.AreEqual(1, canExecuteChangedCalled);
#elif NETFX_CORE
            Assert.AreEqual(1, canExecuteChangedCalled);
#elif PORTABLE
            Assert.AreEqual(1, canExecuteChangedCalled);
#else
            // In WPF, cannot trigger the CanExecuteChanged event like this
            Assert.AreEqual(0, canExecuteChangedCalled);
#endif
        }
 public MainWindowViewModel()
 {
     SearchCommand = new RelayCommand(() => OnSearchExecuted(), () => !string.IsNullOrWhiteSpace(_searchText));
     ClearCommand = new RelayCommand(() => SeriesCollection.Clear(), () => SeriesCollection.Count > 0);
     SerializeCommand = new RelayCommand(OnSerializeExecuted, () => SeriesCollection.Count > 0);
     SeriesCollection.CollectionChanged += (s, e) =>
     {
         ClearCommand.RaiseCanExecuteChanged();
         SerializeCommand.RaiseCanExecuteChanged();
     };
 }
        public void TestCanExecuteChanged()
        {
            var command = new RelayCommand(() =>
            {
            },
                                           () => true);

            var canExecuteChangedCalled = 0;

            var canExecuteChangedEventHandler = new EventHandler((s, e) => canExecuteChangedCalled++);

            command.CanExecuteChanged += canExecuteChangedEventHandler;

            command.RaiseCanExecuteChanged();

            Assert.AreEqual(1, canExecuteChangedCalled);

            command.CanExecuteChanged -= canExecuteChangedEventHandler;
            command.RaiseCanExecuteChanged();

            Assert.AreEqual(1, canExecuteChangedCalled);
        }
        public MainViewModel(IMessageBoxService messageBoxService)
        {
            _messageBoxService = messageBoxService;

            AddItemCommand = new RelayCommand(() =>
            {
                Items.Add(DateTime.Now.ToString());

                EnableSelectionCommand.RaiseCanExecuteChanged();
            });

            EnableSelectionCommand = new RelayCommand(() =>
            {
                IsSelectionEnabled = true;
            }, () => Items.Count > 0);

            DeleteItemsCommand = new RelayCommand<System.Collections.IList>(items =>
            {
                var itemsToRemove = items
                    .Cast<string>()
                    .ToArray();

                foreach (var itemToRemove in itemsToRemove)
                {
                    Items.Remove(itemToRemove);
                }

                EnableSelectionCommand.RaiseCanExecuteChanged();

                IsSelectionEnabled = false;
            });

            BackKeyPressCommand = new RelayCommand<CancelEventArgs>(e =>
            {
                if (IsSelectionEnabled)
                {
                    IsSelectionEnabled = false;

                    e.Cancel = true;
                }
            });

            AboutCommand = new RelayCommand(() =>
            {
                _messageBoxService.Show("Cimbalino Windows Phone Toolkit Bindable Application Bar Sample", "About");
            });

            Items = new ObservableCollection<string>();
        }
        //create a dialog that accepts a playlist name and returns the new playlist
        public async Task<Playlist> CreateNewPlaylist()
        {
            Playlist play = new Playlist { PlaylistId = Guid.NewGuid() };
            var dialog = new ContentDialog()
            {
                Title = "New Playlist",
            };
            var panel = new StackPanel();
            var tb = new TextBox
            {
                PlaceholderText = "Enter the new playlist's name",
                Margin = new Windows.UI.Xaml.Thickness { Top = 10 }
            };
            panel.Children.Add(tb);
            dialog.Content = panel;

            dialog.PrimaryButtonText = "Save";
            var cmd = new RelayCommand(() =>
            {
                play.PlaylistName = tb.Text;
                
            }, () => CanSave(tb.Text));

            dialog.IsPrimaryButtonEnabled = false;
            dialog.PrimaryButtonCommand = cmd;
            dialog.SecondaryButtonText = "Cancel";
            dialog.RequestedTheme = (ElementTheme)new Converters.BooleanToThemeConverter().Convert(Settings.UseDarkTheme, null, null, null);
            tb.TextChanged += delegate
            {
                cmd.RaiseCanExecuteChanged();
                if (tb.Text.Trim().Length > 0)
                {
                    dialog.IsPrimaryButtonEnabled = true;
                }
                else
                {
                    dialog.IsPrimaryButtonEnabled = false;
                }

            };

            var result = await dialog.ShowAsync();
            if (result == ContentDialogResult.Primary)
            {
                return play;
            }
            return null;
        }
        public SendEDIDManager_VM()
        {
            CmdRefashSelectedType = new RelayCommand(OnCmdRefreshSelectedType, CanCmdRefreshSelectedType);
            CmdSetSelectedType = new RelayCommand(OnCmdSetSelectedType, CanCmdSetSelectedType);
            SenderDisplayInfoList.CollectionChanged += (s, e) =>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                        CmdRefashSelectedType.RaiseCanExecuteChanged();
                        CmdSetSelectedType.RaiseCanExecuteChanged();
                        break;
                    default:
                        break;
                }
            };


            if (this.IsInDesignMode)
            {
                DataGradViewItem data = new DataGradViewItem();
                data.SenderIndex = 0;
                data.SerialPort = "COM1";
                data.RefreshRate = 60;
                data.Reslution = "1024*768";
                data.IsChecked = false;
                SenderDisplayInfoList.Add(data);
                data = new DataGradViewItem();
                data.SenderIndex = 1;
                data.SerialPort = "COM1";
                data.RefreshRate = 60;
                data.Reslution = "1024*768";
                data.IsChecked = false;
                SenderDisplayInfoList.Add(data);

                data = new DataGradViewItem();
                data.SenderIndex = 2;
                data.SerialPort = "COM2";
                data.RefreshRate = 60;
                data.Reslution = "1024*768";
                data.IsChecked = false;
                SenderDisplayInfoList.Add(data);
                data = new DataGradViewItem();
                data.SenderIndex = 3;
                data.SerialPort = "COM2";
                data.RefreshRate = 60;
                data.Reslution = "1024*768";
                data.IsChecked = false;
                SenderDisplayInfoList.Add(data);

            }
            else
            {
                Initialize();
            }
        }
        private void InitializeView()
        {
            SearchForPositionCommand = new RelayCommand(() =>
            {
                PositionsFoundHashes.Clear();

                var found = _positionService.Find(SearchField);

                if (PositionsFound.Count > 0)
                {
                    var index = 0;
                    do
                    {
                        if (PositionsFound[index] == SelectedPosition)
                            index++;
                        else
                            PositionsFound.RemoveAt(index);
                    } while (index < PositionsFound.Count);
                }

                foreach(var position in found)
                {
                    if (SelectedPosition == null || position.Id != SelectedPosition.Id)
                        PositionsFound.Add(position);
                }

                var hashes = PositionsFound.Select(x => (long)(x.Title + x.Description).GetHashCode());
                PositionsFoundHashes.AddRange(hashes);
            }, () => { return true; });

            SavePositionCommand = new RelayCommand(
                () => {
                    _positionService.Save(SelectedPosition);

                    PositionsFoundHashes.Clear();
                    var hashes = PositionsFound.Select(x => (long)(x.Title + x.Description).GetHashCode());
                    PositionsFoundHashes.AddRange(hashes);

                    SearchField = SelectedPosition.Title;

                    SavePositionCommand.RaiseCanExecuteChanged();
                }
                ,() =>
                {
                    var selectedPositionHash = SelectedPosition == null
                        ? new Random().Next()
                        : (SelectedPosition.Title + SelectedPosition.Description).GetHashCode();

                    return SelectedPosition != null
                        && SelectedPosition.Title != null && SelectedPosition.Title.Length > 1
                        && SelectedPosition.Description != null && SelectedPosition.Description.Length > 1
                        && !PositionsFoundHashes.Contains(selectedPositionHash);
                }
            );

            ClearPositionCommand = new RelayCommand(() =>
            {
                PositionsFound.Clear();
                SelectedPosition = new Position();
                SearchField = "";

            });

            UpdateCanSaveCommand = new RelayCommand(() =>
            {
                SavePositionCommand.RaiseCanExecuteChanged();
            });

            ExpandComboBoxCommand = new RelayCommand(() =>
            {
                ShowMatchingPositions = true;
            });

            SelectedPosition = new Position();
            PositionsFound = new ObservableCollection<Position>();
        }
        public TreemapViewModel(TreemapChart treemap, ChartData data, TreemapAlgorithm algo) : this()
        {
            Treemap = treemap;
            Data = data;
            Columns = Data.ColumnNames;

            Algorithm = algo;
            InitParameters();
            InitColorViewModels();
            SetColorViewModel();

            Messenger.Default.Register<PropertyChangedMessageBase>
            (
                 this, true,
                 (m) =>
                 {
                     if (IsDead || !IsSentBySelf(m.Sender))
                         return;

                     if (m.PropertyName == "ColorMethod")
                         SetColorViewModel();

                     ShowLegendDecimalPlaces = LegendFormatType != FormatType.Text;

                     if (!LockRefresh)
                         DrawChart();
                 }
            );

            RefreshCommand = new RelayCommand<object>(_ => DrawChart(true));

            DeleteCommand = new RelayCommand<object>(
                _ =>
                {
                    LockRefresh = true;
                    Indexes.RemoveAt(Indexes.Count - 1);
                    Indexes.Last().AsChildIndex();

                    DeleteCommand.RaiseCanExecuteChanged();
                    AddCommand.RaiseCanExecuteChanged();

                    DrawChart();
                    LockRefresh = false;
                },
                _ => Indexes.Count > 1);

            AddCommand = new RelayCommand<object>(
                _ =>
                {
                    LockRefresh = true;
                    Indexes.Last().AsParentIndex();

                    string freeColumn = Columns.Where(c => !Indexes.Select(i => i.Column).Contains(c)).First();
                    Indexes.Add(new TreemapIndexViewModel(freeColumn));

                    DeleteCommand.RaiseCanExecuteChanged();
                    AddCommand.RaiseCanExecuteChanged();

                    DrawChart();
                    LockRefresh = false;
                },
                _ => Indexes.Count < Columns.Count);

            DrawChart();
        }
        public FormulaEditorViewModel()
        {
            KeyPressedCommand = new RelayCommand<FormulaKeyEventArgs>(KeyPressedAction);
            ShowErrorPressedCommand = new RelayCommand(ShowErrorPressedAction, ShowErrorPressedCommand_CanExecute);
            UndoCommand = new RelayCommand(UndoAction, UndoCommand_CanExecute);
            RedoCommand = new RelayCommand(RedoAction, RedoCommand_CanExecute);
            SensorCommand = new RelayCommand(SensorAction);
            CompleteTokenCommand = new RelayCommand<int>(CompleteTokenAction);
            SaveCommand = new RelayCommand(SaveAction);
            CancelCommand = new RelayCommand(CancelAction);
            
            Messenger.Default.Register<GenericMessage<Sprite>>(this, ViewModelMessagingToken.CurrentSpriteChangedListener, SelectedSpriteChangedMessageAction);
            Messenger.Default.Register<GenericMessage<Program>>(this, ViewModelMessagingToken.CurrentProgramChangedListener, CurrentProgramChangedMessageAction);

            _editor.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == GetPropertyName(() => _editor.Formula)) RaisePropertyChanged(() => Formula);
                if (e.PropertyName == GetPropertyName(() => _editor.Tokens)) RaisePropertyChanged(() => Tokens);
                if (e.PropertyName == GetPropertyName(() => _editor.IsTokensEmpty)) RaisePropertyChanged(() => IsTokensEmpty);
                if (e.PropertyName == GetPropertyName(() => _editor.CaretIndex)) RaisePropertyChanged(() => CaretIndex);
                if (e.PropertyName == GetPropertyName(() => _editor.SelectionStart)) RaisePropertyChanged(() => SelectionStart);
                if (e.PropertyName == GetPropertyName(() => _editor.SelectionLength)) RaisePropertyChanged(() => SelectionLength);
                if (e.PropertyName == GetPropertyName(() => _editor.CanUndo))
                {
                    RaisePropertyChanged(() => CanUndo);
                    UndoCommand.RaiseCanExecuteChanged();
                }
                if (e.PropertyName == GetPropertyName(() => _editor.CanRedo))
                {
                    RaisePropertyChanged(() => CanRedo);
                    RedoCommand.RaiseCanExecuteChanged();
                }
                if (e.PropertyName == GetPropertyName(() => _editor.CanDelete)) RaisePropertyChanged(() => CanDelete);
                if (e.PropertyName == GetPropertyName(() => _editor.HasError))
                {
                    RaisePropertyChanged(() => HasError);
                    ShowErrorPressedCommand.RaiseCanExecuteChanged();
                }
                if (e.PropertyName == GetPropertyName(() => _editor.HasError)) RaisePropertyChanged(() => CanEvaluate);
                if (e.PropertyName == GetPropertyName(() => _editor.ParsingError)) RaisePropertyChanged(() => ParsingError);
            };

            _keyboardViewModel = ServiceLocator.ViewModelLocator.FormulaKeyboardViewModel;
            //_keyboardViewModel.PropertyChanged += (sender, e) =>
            //{
            //    if (e.PropertyName == GetPropertyName(() => _keyboardViewModel.IsAddLocalVariableButtonVisible)) RaisePropertyChanged(() => IsAddLocalVariableButtonVisible);
            //    if (e.PropertyName == GetPropertyName(() => _keyboardViewModel.IsAddGlobalVariableButtonVisible)) RaisePropertyChanged(() => IsAddGlobalVariableButtonVisible);
            //};
            _sensorsAreActive = false;
        }