protected override void OnDropDownOpened(EventArgs e)
{
base.OnDropDownOpened(e);
Mouse.Capture(this, CaptureMode.SubTree);
if (SelectedItem != null) Keyboard.Focus(ItemContainerGenerator.ContainerFromItem(SelectedItem) as IInputElement);
focusedElement = Keyboard.FocusedElement;
focusedElement.LostKeyboardFocus += OnFocusedElementLostKeyboardFocus;
canSizeX = true;
canSizeY = true;
galleryPanel.Width = double.NaN;
scrollViewer.Height = double.NaN;
FrameworkElement popupChild = popup.Child as FrameworkElement;
scrollViewer.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
popupChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
popup.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double heightDelta = popupChild.DesiredSize.Height - scrollViewer.DesiredSize.Height;
double initialHeight = Math.Min(RibbonControl.GetControlWorkArea(this).Height * 2 / 3, MaxDropDownHeight);
if (!double.IsNaN(DropDownHeight)) initialHeight = Math.Min(DropDownHeight, MaxDropDownHeight);
if (scrollViewer.DesiredSize.Height > initialHeight)
{
scrollViewer.Height = initialHeight;
}
else initialHeight = scrollViewer.DesiredSize.Height;
Rect monitor = RibbonControl.GetControlMonitor(this);
double delta = monitor.Bottom - this.PointToScreen(new Point()).Y - ActualHeight - initialHeight - heightDelta;
if (delta >= 0) ShowPopupOnTop = false;
else
{
double deltaTop = this.PointToScreen(new Point()).Y - initialHeight - heightDelta - monitor.Top;
if (deltaTop > delta) ShowPopupOnTop = true;
else ShowPopupOnTop = false;
if (deltaTop < 0)
{
delta = Math.Max(Math.Abs(delta), Math.Abs(deltaTop));
if (delta > galleryPanel.GetItemSize().Height)
{
scrollViewer.Height = delta;
}
else
{
canSizeY = false;
scrollViewer.Height = galleryPanel.GetItemSize().Height;
}
}
}
popupChild.UpdateLayout();
}