Is it possible to show ContextFlyout over UIElement?

BitSmithy 2,141 Reputation points
2020-04-17T09:57:15.447+00:00

Hello,

Is it possible to show ContextFlyout over UIElement? If yes please write me example code.

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. 帕菲菲 141 Reputation points
    2020-04-21T06:17:20.697+00:00
    <Image>
     <Image.ContextFlyout>
     <MenuFlyout>
     <MenuFlyoutItem/>
     <MenuFlyoutItem/>
     </MenuFlyout>
     </Image.ContextFlyout>
    </Image>
    

    I believe this XAML works for most contexts. Change <Image> node to your UIElement and set other necessary properties as you need. In most cases, you don't even have to handle the RightTapped event of your UIElement; instead you handle the MenuFlyoutItem.Click event.

    0 comments No comments

  2. Daniele 1,996 Reputation points
    2020-04-22T06:29:57.34+00:00

    @BitSmithy , your requirement are not so much clear to me: an image with the expected result would be helpful to understand better.

    Anyway, below some code show a transparent Flyout at -5, -5 relative to an UIElement.

    XAML

       <Border Width="100" Height="80" Background="Black">  
       	<Border.ContextFlyout>  
       		<Flyout Opened="FlyoutBase_OnOpened">  
       			<Flyout.FlyoutPresenterStyle>  
       				<Style TargetType="FlyoutPresenter">  
       					<Setter Property="Background" Value="#55FF0000"/>  
       				</Style>  
       			</Flyout.FlyoutPresenterStyle>  
       			<Grid>  
       				<TextBlock Foreground="Red">Flyout</TextBlock>  
       			</Grid>  
       		</Flyout>  
       	</Border.ContextFlyout>  
       </Border>  
    

    FlyoutBase_OnOpened

       private void FlyoutBase_OnOpened(object sender, object e)  
       {  
       	var flyout = (Flyout)sender;  
         
       	Point transformPoint = flyout.Target.TransformToVisual(Window.Current.Content).TransformPoint(new Point(-5, -5));  
         
       	var flyoutContent = (FrameworkElement) flyout.Content;  
         
       	var flyoutPresenter = VisualTreeHelperUtils.Parent<FlyoutPresenter>(flyoutContent);  
         
       	Popup popup = VisualTreeHelper.GetOpenPopups(Window.Current).FirstOrDefault(p => p.Child == flyoutPresenter);  
       	if (popup == null) return;  
       	popup.HorizontalOffset = transformPoint.X;  
       	popup.VerticalOffset = transformPoint.Y;  
       }  
    

    VisualTreeHelperUtils.Parent<T>

       public static T Parent<T>(DependencyObject dependencyObject)  
       {  
       	while (true)  
       	{  
       		DependencyObject parent = VisualTreeHelper.GetParent(dependencyObject);  
       		switch (parent)  
       		{  
       			case null:  
       				return default(T);  
       			case T typedParent:  
       				return typedParent;  
       			default:  
       				dependencyObject = parent;  
       				continue;  
       		}  
       	}  
       }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.