/// <summary> /// Set the IsSubmenuOpen property value at the right time. /// </summary> private static object CoerceIsSubmenuOpen(DependencyObject element, object value) { SplitButton splitButton = element as SplitButton; if ((bool)value) { if (!splitButton.IsLoaded) { splitButton.Loaded += delegate(object sender, RoutedEventArgs args) { splitButton.CoerceValue(IsSubmenuOpenProperty); }; return(BooleanBoxes.FalseBox); } } return((bool)value && splitButton.HasItems); }
private static void OnIsSubmenuOpenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { SplitButton splitButton = sender as SplitButton; if ((bool)args.NewValue) { openSplitButton = splitButton; // Handle keyboard events as long as the popup is displayed var window = Window.GetWindow(splitButton); window.PreviewKeyDown += OnPreviewKeyDown; if (Mouse.Captured != splitButton) { Mouse.Capture(splitButton, CaptureMode.SubTree); Mouse.AddLostMouseCaptureHandler(splitButton, OnLostMouseCapture); } } else { // Remove popup keyboard handler var window = Window.GetWindow(splitButton); window.PreviewKeyDown -= OnPreviewKeyDown; openSplitButton = null; if (Mouse.Captured == splitButton) { Mouse.Capture(null); } if (splitButton.IsKeyboardFocused) { splitButton.Focus(); } } }