コード例 #1
0
        private static void OnOpened(object sender, EventArgs e)
        {
            var popup = sender as Popup;

            if (popup != null)
            {
                var target = popup.PlacementTarget as FrameworkElement;
                if (target != null)
                {
                    if (popup.Child != null)
                    {
                        var root = VisualTreeHelperExtensions.FindTopLevelParent(popup.Child) as Visual;
                        if (root != null)
                        {
                            var realPosition                = root.PointToScreen(new Point(0d, 0d));
                            var calculatedPosition          = target.PointToScreen(new Point(0d + popup.HorizontalOffset, target.ActualHeight - popup.VerticalOffset));
                            var isDefaultHorizontalPosition = Math.Abs(realPosition.X - calculatedPosition.X) < double.Epsilon;
                            var isDefaultVerticalPosition   = Math.Abs(realPosition.Y - calculatedPosition.Y) < double.Epsilon;
                            SetIsDefaultHorizontalPosition(popup, isDefaultHorizontalPosition);
                            SetIsDefaultVerticalPosition(popup, isDefaultVerticalPosition);
                        }
                    }
                }
            }
        }
コード例 #2
0
 private void OnDropDownOpened(object sender, EventArgs e)
 {
     if (HasSubmenu)
     {
         Mouse.Capture((IInputElement)VisualTreeHelperExtensions.FindTopLevelParent(Submenu), CaptureMode.SubTree);
         OnDropDownOpened(e);
     }
 }
コード例 #3
0
 private void OnDropDownOpened(object sender, EventArgs e)
 {
     if (HasSubmenu)
     {
         // HasSubmenu ensures that Submenu isn't null
         Contract.Assume(Submenu != null);
         Mouse.Capture((IInputElement)VisualTreeHelperExtensions.FindTopLevelParent(Submenu), CaptureMode.SubTree);
         OnDropDownOpened(e);
     }
 }
コード例 #4
0
        private void ApplyTemplateInternal()
        {
            if (Template != null)
            {
                if (_popup != null)
                {
                    _popup.Child = null;
                    if (Submenu != null)
                    {
                        var popupRootInstance = VisualTreeHelperExtensions.FindTopLevelParent(Submenu);
                        if (popupRootInstance != null)
                        {
                            var presentationFramework = Assembly.GetAssembly(typeof(System.Windows.Window));
                            if (presentationFramework != null)
                            {
                                var popupRoot = presentationFramework.GetType("System.Windows.Controls.Primitives.PopupRoot");
                                if (popupRoot != null)
                                {
                                    var popupRootChild = popupRoot.GetProperty("Child",
                                                                               BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.NonPublic);
                                    if (popupRootChild != null)
                                    {
                                        popupRootChild.SetValue(popupRootInstance, null, null);
                                    }
                                }
                            }
                        }
                    }
                    _popup.Closed -= OnDropDownClosed;
                    _popup.Opened -= OnDropDownOpened;
                    _popup.CustomPopupPlacementCallback = null;
                }

                // Bug in Code Contracts static checker: Template is already checked to null
                Contract.Assume(Template != null);
                _popup = Template.FindName(PopupName, this) as Popup;
                if (_popup == null)
                {
                    Trace.TraceError(PopupName + " not found.");
                }
                else
                {
                    _popup.CustomPopupPlacementCallback = PlacePopup;
                    _popup.Opened += OnDropDownOpened;
                    _popup.Closed += OnDropDownClosed;
                    if (Submenu != null)
                    {
                        _popup.Child = Submenu;
                    }
                }
            }
        }