private void OKBtn_Click(object sender, RoutedEventArgs e) { var d = (Device)DeviceCmboBx.SelectedItem; if (d == null) { DeviceCmboBx.Focus(); DeviceCmboBx.BorderBrush = new SolidColorBrush(Colors.Red); var anim = new ColorAnimation(Color.FromArgb(255, 112, 112, 112), new Duration(TimeSpan.FromSeconds(1.5))); DeviceCmboBx.BorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, anim); e.Handled = false; return; } var dv = (DeviceValue)ValueCmboBx.SelectedItem; if (dv == null) { ValueCmboBx.Focus(); ValueCmboBx.BorderBrush = new SolidColorBrush(Colors.Red); var anim = new ColorAnimation(Color.FromArgb(255, 112, 112, 112), new Duration(TimeSpan.FromSeconds(1.5))); ValueCmboBx.BorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, anim); e.Handled = false; return; } Trigger.DeviceValue = dv; if (Trigger.CommandId == 0) { AddUpdateCommand.Focus(); AddUpdateCommand.BorderBrush = new SolidColorBrush(Colors.Red); var anim = new ColorAnimation(Color.FromArgb(255, 112, 112, 112), new Duration(TimeSpan.FromSeconds(1.5))); AddUpdateCommand.BorderBrush.BeginAnimation(SolidColorBrush.ColorProperty, anim); e.Handled = false; return; } if (string.IsNullOrEmpty(ValueTxtBx.Text)) { ValueTxtBx.Focus(); ValueTxtBx.Background = new SolidColorBrush(Colors.Pink); var anim = new ColorAnimation(Colors.White, new Duration(TimeSpan.FromSeconds(1.5))); ValueTxtBx.Background.BeginAnimation(SolidColorBrush.ColorProperty, anim); e.Handled = false; return; } Trigger.Value = ValueTxtBx.Text; Trigger.Operator = (TriggerOperator)OperatorCmboBx.SelectedItem; //Update the description Trigger.SetTriggerDescription(); Canceled = false; Close(); }
/// <summary> /// Create displaying everything. Having a key as null is fine - then everything /// starts out blank. /// </summary> /// <param name="key"></param> public AddOrUpdateIndicoApiKeyViewModel(IndicoApiKey key) { if (key == null) { SiteName = ""; ApiKey = ""; SecretKey = ""; } else { SiteName = key.Site; ApiKey = key.ApiKey; SecretKey = key.SecretKey; } // Setup the add and update commands to work correctly. // A missing secret key is allowed. var addCanExe = this.WhenAny(x => x.SiteName, x => x.ApiKey, x => x.SecretKey, (site, apik, seck) => Tuple.Create(site.Value, apik.Value, seck.Value)) .Select(x => !string.IsNullOrWhiteSpace(x.Item1) && !string.IsNullOrWhiteSpace(x.Item2)); AddUpdateCommand = ReactiveCommand.Create(addCanExe); var isKnownSite = new ReplaySubject <bool>(1); DeleteCommand = ReactiveCommand.Create(isKnownSite.StartWith(false)); Observable.Merge( this.WhenAny(x => x.SiteName, x => x.Value), AddUpdateCommand.IsExecuting.Where(isexe => isexe == false).Select(_ => SiteName), DeleteCommand.IsExecuting.Where(isexe => isexe == false).Select(_ => SiteName) ) .Select(sname => IndicoApiKeyAccess.GetKey(sname) != null) .Subscribe(gotit => isKnownSite.OnNext(gotit)); isKnownSite .Select(known => known ? "Update" : "Add") .ToProperty(this, x => x.AddOrUpdateText, out _addOrUpdateText, "Add"); // Add, update, or remove AddUpdateCommand .Subscribe(o => IndicoApiKeyAccess.UpdateKey(new IndicoApiKey() { Site = SiteName, ApiKey = ApiKey, SecretKey = SecretKey })); DeleteCommand .Subscribe(o => IndicoApiKeyAccess.RemoveKey(SiteName)); }
void UpdateCommands() { AddUpdateCommand.RaiseCanExecuteChanged(); CancelCommand.RaiseCanExecuteChanged(); }