void UpdatePopupSizeAndPosition (object sender, EventArgs args)
{
if (_popup == null)
return;
FrameworkElement child = _popup.RealChild as FrameworkElement;
if (child == null)
return;
child.MinWidth = ActualWidth;
var root = Application.Current.Host.Content;
if (root == null)
return;
GeneralTransform xform;
try {
xform = TransformToVisual (null);
}
catch (ArgumentException) {
// exception is raised if the combobox is no longer in the visual tree
// LayoutUpdated -= UpdatePopupSizeAndPosition;
return;
}
Point bottom_right = new Point (child.ActualWidth, ActualHeight + child.ActualHeight);
bottom_right = xform.Transform (bottom_right);
if (bottom_right.X > root.ActualWidth) {
_popup.HorizontalOffset = root.ActualWidth - bottom_right.X;
} else {
_popup.HorizontalOffset = 0;
}
if (bottom_right.Y > root.ActualHeight) {
_popup.VerticalOffset = -child.ActualHeight;
} else {
_popup.VerticalOffset = RenderSize.Height;
}
// Silverlight does not resize its dropdown properly when the available height is altered.
// This means that if you open a dropdown while your browser window is small, you end up
// with a dropdown that is far too small forever. Instead of this we will resize the dropdown
// to a more usable height.
UpdatePopupMaxHeight (MaxDropDownHeight);
}