/// <summary> /// Creates the control for notification state item. /// </summary> /// <param name="notificationStateItem">The notification state item.</param> /// <param name="assignGlobalSettingsEnabledDependency">if set to <c>true</c> [assign global settings enabled dependency].</param> /// <returns>The <see cref="StackPanel"/> with the toggle button and caption.</returns> private StackPanel CreateControlForNotificationStateItem(NotificationStateItem notificationStateItem, bool assignGlobalSettingsEnabledDependency = true) { var itemStackPanel = new StackPanel(); itemStackPanel.Orientation = Orientation.Horizontal; itemStackPanel.Margin = new Thickness(2, 6, 2, 0); var toggleButton = new ToggleButton(); var style = Application.Current.FindResource("MaterialDesignSwitchAccentToggleButton") as Style; toggleButton.Style = style; var toogleButtonBinding = new Binding(nameof(NotificationStateItem.IsActive)); toogleButtonBinding.Source = notificationStateItem; toogleButtonBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; toggleButton.SetBinding(ToggleButton.IsCheckedProperty, toogleButtonBinding); itemStackPanel.Children.Add(toggleButton); var label = new Label(); label.Content = notificationStateItem.Caption; if (assignGlobalSettingsEnabledDependency) { var toogleButtonEnabledBinding = new Binding(nameof(NotificationStateItem.IsNotUseGlobalSettingsForParentListActive)); toogleButtonEnabledBinding.Source = notificationStateItem; toogleButtonEnabledBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; toggleButton.SetBinding(UIElement.IsEnabledProperty, toogleButtonEnabledBinding); } itemStackPanel.Children.Add(label); return(itemStackPanel); }
/// <summary> /// Applies the <see cref="IConfigurationItem" />to <see cref="NotificationsState" />s. /// </summary> /// <param name="notificationsState">The notification state.</param> /// <param name="configItem">The configuration item.</param> /// <param name="configItemAttribute">The configuration item attribute.</param> /// <param name="expander">The expander control.</param> private void ApplyConfigurationItemToNotificationState(NotificationsState notificationsState, IConfigurationItem configItem, ConfigurationItemAttribute configItemAttribute, Expander expander) { var configItemValue = configItem.GetOrCreateConnectorNotificationConfiguration(); var observationStates = Enum.GetValues(typeof(ObservationState)).Cast <ObservationState>().ToList(); if (configItemAttribute is NotificationConfigurationItemAttribute notificationConfigurationItemAttribute) { this.RemoveNotSupportedObservationStates(observationStates, notificationConfigurationItemAttribute); } var useGlobalSettingsNewState = configItemValue.UseGlobalNotificationSettings; if (useGlobalSettingsNewState != notificationsState.UseGlobalSettings.IsActive) { notificationsState.UseGlobalSettings.IsActive = useGlobalSettingsNewState; } var onlyIfStateChangedSettingsNewState = configItemValue.OnlyIfChanged; if (onlyIfStateChangedSettingsNewState != notificationsState.OnlyIfStateChanged.IsActive) { notificationsState.OnlyIfStateChanged.IsActive = onlyIfStateChangedSettingsNewState; } foreach (var observationState in observationStates) { var notificationStateItem = notificationsState.ItemsState.FirstOrDefault(n => n.ObservationState == observationState); if (notificationStateItem == null) { notificationStateItem = new NotificationStateItem(notificationsState); notificationStateItem.IsActive = configItemValue.AsObservationStateFlag(observationState); notificationStateItem.ObservationState = observationState; notificationStateItem.Caption = observationState.ToString(); // TODO load from resources notificationStateItem.PropertyChanged += (s, e) => this.NotificationStateChanged(notificationStateItem, configItem, e.PropertyName); notificationStateItem.PropertyChanged += (s, e) => this.UpdateExpanderHeader(expander, notificationsState); notificationsState.ItemsState.Add(notificationStateItem); } else { var newActiveState = configItemValue.UseGlobalNotificationSettings; if (notificationStateItem.IsActive != newActiveState) { notificationStateItem.IsActive = configItemValue.AsObservationStateFlag(observationState); } } } }
/// <summary> /// Handled the <see cref="NotificationStateItem"/> changed. /// Updates the <see cref="IConfigurationItem"/>. /// </summary> /// <param name="notificationStateItem">State of the notification.</param> /// <param name="configItem">The configuration item.</param> /// <param name="propertyName">Name of the property.</param> private void NotificationStateChanged(NotificationStateItem notificationStateItem, IConfigurationItem configItem, string propertyName) { if (propertyName == nameof(NotificationStateItem.IsNotUseGlobalSettingsForParentListActive)) { return; } var connectorNotificationConfiguration = new ConnectorNotificationConfiguration(); connectorNotificationConfiguration.UseGlobalNotificationSettings = notificationStateItem.NotificationsState.UseGlobalSettings.IsActive; connectorNotificationConfiguration.OnlyIfChanged = notificationStateItem.NotificationsState.OnlyIfStateChanged.IsActive; foreach (var item in notificationStateItem.NotificationsState.ItemsState) { connectorNotificationConfiguration.AssignFromObeservationStateActivity(item.ObservationState, item.IsActive); } var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }; configItem.Value = JsonConvert.SerializeObject(connectorNotificationConfiguration, settings); }