Example #1
0
    /// <summary>
    /// Get the item under the mouse
    /// </summary>
    /// <param name="senderElement">The clicked or hovered element</param>
    /// <param name="e"></param>
    /// <param name="returnSelectableItemIfAny">Return the clicked or hovered item inside the trigger element if the latter is a DataGrid, a ListBox or a TreeView</param>
    /// <param name="selectItemIfSelectable">Select the item if lies in a ListBox, Datagrid or TreeView</param>
    /// <returns>The item under the mouse</returns>
    private FrameworkElement GetItemUnderMouse(FrameworkElement senderElement, MouseEventArgs e, bool returnSelectableItemIfAny, bool selectItemIfSelectable) {
      VisualTreeElementsUnderMouse = VisualTreeHelper.FindElementsInHostCoordinates(
        e.GetPosition(Application.Current.RootVisual),
        senderElement.GetVisualAncestors().Last() as FrameworkElement);

      return PopupMenuUtils.GetItemUnderMouse(VisualTreeElementsUnderMouse, senderElement, returnSelectableItemIfAny, selectItemIfSelectable);
    }
        /// <summary>
        /// Indicates whether the specified framework element
        /// is within the bounds of the application's root visual.
        /// </summary>
        /// <param name="element">The framework element.</param>
        /// <returns>
        /// True if the rectangular bounds of the framework element
        /// are completely outside the bounds of the application's root visual.
        /// </returns>
        private static bool IsOnScreen(FrameworkElement element)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;

            if (root == null)
            {
                return false;
            }

            GeneralTransform generalTransform;
            double height = root.ActualHeight;
            double width = root.ActualWidth;

            try
            {
                generalTransform = element.TransformToVisual(root);
            }
            catch (ArgumentException)
            {
                return false;
            }

            Rect bounds = new Rect(
                generalTransform.Transform(Origin),
                generalTransform.Transform(new Point(element.ActualWidth, element.ActualHeight)));

            bool isParentTransparent = false;
            IList<FrameworkElement> ancestors = element.GetVisualAncestors().ToList();

            if (ancestors != null)
            {
                for (int i = 0; i < ancestors.Count; i++)
                {
                    if (ancestors[i].Opacity <= 0.001)
                    {
                        isParentTransparent = true;
                        break;
                    }
                }
            }

            return (bounds.Bottom > 0) && (bounds.Top < height) 
                && (bounds.Right > 0) && (bounds.Left < width) 
                && !isParentTransparent;
        }
        /// <summary>
        /// Retrieves or creates the merged style dictionary of an element.
        /// </summary>
        /// <param name="styledElement">A styled element.</param>
        /// <returns>The merged dictionary of the element.</returns>
        private static BaseMergedStyleDictionary GetMergedStyleDictionary(FrameworkElement styledElement)
        {
            Debug.Assert(styledElement != null, "styledElement cannot be null.");

            IList<FrameworkElement> ancestorsInclusive = styledElement.GetVisualAncestors().Append(styledElement).Reverse().ToList();

            IEnumerable<FrameworkElement> ancestorsTopToBottom = ancestorsInclusive;

            BaseMergedStyleDictionary initialDictionary = null;
            if (UseApplicationResources)
            {
                initialDictionary = new MergedStyleResourceDictionary(ApplicationExternalResourceDictionary ?? Application.Current.Resources);
            }
            else
            {
                FrameworkElement topLevelElement = ancestorsInclusive[0];
                initialDictionary = new MergedStyleResourceDictionary(ImplicitStyleManager.GetExternalResourceDictionary(topLevelElement) ?? topLevelElement.Resources);

                ancestorsTopToBottom = ancestorsInclusive.Skip(1);
            }

            BaseMergedStyleDictionary styleDictionary =
                ancestorsTopToBottom.Aggregate(
                    initialDictionary,
                    (dictionary, ancestor) => new MergedStyleResourceDictionary(ImplicitStyleManager.GetExternalResourceDictionary(ancestor) ?? ancestor.Resources) { Parent = dictionary });

            return styleDictionary;
        }