Popup from code behind in WPF not working ( On resize and switching windows)

19,796

Solution 1

you have to tell the popup to display itself, too.

codePopup.IsOpen = true;

see this blog for a bit more info.

[EDIT]
basically, your popup is not "tied" (or "owned") by any parent, it is independent of any other window and/or control (etc.) Unfortunatly, there is not any easy way to get around this.

You should probably download the Popup Position Sample from MSDN here.

The code sample uses the class CustomPopupPlacement with a Rect object, and binds to horizontal and vertical offsets to move the Popup.

Solution 2

If you want popup should close automatically when you click outside of it then set codePopup.StaysOpen = false.So that it will close when you click outside of it.

Share:
19,796
Gurucharan Balakuntla Maheshku
Author by

Gurucharan Balakuntla Maheshku

“My objective is to provide solutions which can change the way people think and have a great impact on them” Software Engineering and Technology leader with 14+ years of experience in the IT industry leading and managing mid-scale to large scale digital systems (as Project Management, Product Management, People Management) across cross-functional teams and delivering highly performant, scalable, and secure systems. Well-versed in managing geographically distributed diverse teams across startups, MNC's and Enterprises. Deep knowledge in ​web architecture ​and ​technologies ​to solve challenging and complex business problems. Vast experience in recruiting and growing a team, instilling the right culture, motivating and retaining the best talents. I have served in diverse domains like Digital Web Apps, eCommerce, Payments, Enterprise applications, CRM, etc. with strong ​product and project management abilities.

Updated on June 15, 2022

Comments

  • Gurucharan Balakuntla Maheshku
    Gurucharan Balakuntla Maheshku almost 2 years

    I want to add a popup on click of a Button in WPF. I dont want to add Popup code in my XAML. It has to be code behind.

    My XAML is as follows::

    <Window x:Class="Test.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Window1" Height="300" Width="300">
       <Grid>
         <Button x:Name="button1" Click="button1_Click">Button</Button>
       </Grid>
    </Window>
    

    My XAML file has a simple button. On click of Button, I am trying to execute the following code.

      private void button1_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
    
            Popup codePopup = new Popup();
            TextBlock popupText = new TextBlock();
            popupText.Text = "Popup Text";
            popupText.Background = Brushes.LightBlue;
            popupText.Foreground = Brushes.Blue;
            codePopup.Child = popupText;
    
            codePopup.PlacementTarget = button;
            codePopup.IsOpen = true;
    
        }
    

    But why is the popup not attaching itself to the window. I mean the popup is displayed even if I switch windows. Also when I resize window, Popup is no longer placed near button??

  • Gurucharan Balakuntla Maheshku
    Gurucharan Balakuntla Maheshku over 13 years
    I added codePopup.IsOpen = true; Thanks its working!! But why is the popup not attaching itself to the window. I mean the popup is displayed even if I switch windows. Also when I resize window, Popup is no longer placed near button??
  • t9mike
    t9mike about 11 years
    @GuruC, did you ever figure out how to create Popup programmatically but have it root itself properly so that popup will close when you click outside it?
  • George Birbilis
    George Birbilis over 9 years
    that sample download is broken, a commenter there says to see msdn.microsoft.com/en-us/library/bb625938.aspx, but that one is missing a download link!