Example #1
0
        internal static void IsChecked_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ToggleButton toggleButton = (ToggleButton)d;

            bool?isChecked = (bool?)e.NewValue;

            // Update DOM appearance (if not using a ControlTemplate):
            if (INTERNAL_VisualTreeManager.IsElementInVisualTree(toggleButton) && !(toggleButton.HasTemplate))
            {
                toggleButton.UpdateDomBasedOnCheckedState(isChecked);
            }

            // Raise user events if the new value is different from the old value:
            bool hasValueRemainedTheSame = (e.NewValue != null && e.NewValue.Equals(e.OldValue)) || (e.NewValue == null && e.OldValue == null);

            if (!hasValueRemainedTheSame) // note: We do not simply check "e.NewValue != e.OldValue" because it does not work as expected for boxed values, so example if both values are "true", it returns "false". //todo: do the same for other similar comparisons in the project.
            {
                if (isChecked == null)
                {
                    toggleButton.OnIndeterminate();
                }
                else if (isChecked == true)
                {
                    toggleButton.OnChecked();
                }
                else // false
                {
                    toggleButton.OnUnchecked();
                }
            }
        }
Example #2
0
        private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ToggleButton button   = (ToggleButton)d;
            bool?        oldValue = (bool?)e.OldValue;
            bool?        newValue = (bool?)e.NewValue;

            if (newValue == true)
            {
                button.OnChecked(new RoutedEventArgs {
                    OriginalSource = button
                });
            }
            else if (newValue == false)
            {
                button.OnUnchecked(new RoutedEventArgs {
                    OriginalSource = button
                });
            }
            else
            {
                button.OnIndeterminate(new RoutedEventArgs {
                    OriginalSource = button
                });
            }

            button.UpdateVisualStates();
        }