none
[UWP][XAML] Adding a Popup programatically (Code behind, C#) RRS feed

  • Question

  • Hi

    I woul'd like to add programatically a popup to my textbox (in my behavior code)

    in WPF it could be done with popup.PlacementTarget = textbox

    How could attach my popup to an existing textbox from code behind?

    Sunday, October 11, 2015 7:01 PM

Answers

  • Hi siudeks,

    Welcome to the Developing Universal Windows apps forum!

    In UWP, I will recommend you use the Flyout control instead of the Popup, after that we can use the Flyout.ShowAt method to add a popup to textbox programatically:

    public void ShowAt(
      FrameworkElement placementTarget
    )
    I have created the following example, please try to refer to:
    In the MainPage.xaml:
     <Page.Resources>
            <Flyout x:Key="MyFlyout">
                <Grid Background="Red">
                    <TextBlock Text="This is a Popup"></TextBlock>
                </Grid>
            </Flyout>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBox Name="MyTextBox" Width="100" Height="100"/>
        </Grid>

    In the MainPage.xaml.cs:
     public MainPage()
            {
                this.InitializeComponent();
                this.Loaded += MainPage_Loaded;
            }
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                Flyout MyFlyout = Resources["MyFlyout"] as Flyout;
                MyFlyout.ShowAt(MyTextBox);
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, October 12, 2015 2:55 AM
    Owner

All replies

  • Hi siudeks,

    Welcome to the Developing Universal Windows apps forum!

    In UWP, I will recommend you use the Flyout control instead of the Popup, after that we can use the Flyout.ShowAt method to add a popup to textbox programatically:

    public void ShowAt(
      FrameworkElement placementTarget
    )
    I have created the following example, please try to refer to:
    In the MainPage.xaml:
     <Page.Resources>
            <Flyout x:Key="MyFlyout">
                <Grid Background="Red">
                    <TextBlock Text="This is a Popup"></TextBlock>
                </Grid>
            </Flyout>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBox Name="MyTextBox" Width="100" Height="100"/>
        </Grid>

    In the MainPage.xaml.cs:
     public MainPage()
            {
                this.InitializeComponent();
                this.Loaded += MainPage_Loaded;
            }
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                Flyout MyFlyout = Resources["MyFlyout"] as Flyout;
                MyFlyout.ShowAt(MyTextBox);
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, October 12, 2015 2:55 AM
    Owner
  • Can we show the flyout at cursor position,like how copy,paste flyout works in textbox?

    Wednesday, June 15, 2016 5:57 AM