How can i make a popup dialog form in WPF?

Mesh Ka 345 Reputation points
2023-09-02T08:51:03.27+00:00

as the heading suggests, i have a wpf application like this, on the MainPanel of my application i display user controls and sometimes i want to display a popup menu like the picture below when for example a user wants to do add some changes in the application settings e.tc, How can i create it in WPF?

13 09-42-25-2112

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,024 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2023-09-04T02:25:12.1033333+00:00

    Hi,@Mesh Ka. Welcome Microsoft Q&A. You could directly open and close a window. You can also create a popup dialog form using a combination of controls and techniques. You could try the example here.

    Create a Custom Dialog UserControl:

    Start by creating a new UserControl in your WPF project. This UserControl will represent your popup dialog.

    Right-click your project in Solution Explorer. Select "Add" -> "User Control (WPF)". Give it a name like "MyPopupDialog.xaml". Design Your Popup UserControl:

    Design your popup dialog within the UserControl just like you would design any other window. You can add buttons, text, input fields, or any other controls that your popup needs. Set the layout and appearance as desired.

    usercontrol:

    
    <UserControl x:Class="YourNamespace.MyPopupDialog"
                ...
                 Height="200" Width="300">
        <Grid>
            <StackPanel>
                <TextBlock Text="Popup Title" FontWeight="Bold" HorizontalAlignment="Center"/>
                <TextBox PlaceholderText="sample"/>
                <Button Content="OK" HorizontalAlignment="Center" Click="OKButton_Click"/>
                <Button Content="Cancel" HorizontalAlignment="Center" Click="CancelButton_Click"/>
            </StackPanel>
        </Grid>
    </UserControl>
    
    

    UserControl.xaml.cs:

    public partial class MyPopupDialog : UserControl
    {
        public MyPopupDialog()
        {
            InitializeComponent();
        }
    
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // Handle OK button click here
            // Close the popup or perform other actions
        }
    
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            // Handle Cancel button click here
            // Close the popup or perform other actions
        }
    }
    
    

    To show the popup dialog, you could create an instance of your MyPopupDialog UserControl and place it inside a Popup control. You can then open and close the Popup as needed.

    MainWindow.xaml:

    
    <Grid>
        <!-- Your main content -->
        <Button Content="Open Popup" Click="OpenPopup_Click"/>
    
        <Popup Name="popup" IsOpen="False" StaysOpen="False" Placement="Center">
            <local:MyPopupDialog/>
        </Popup>
    </Grid>
    
    

    MainWindow.xaml.cs:

    private void OpenPopup_Click(object sender, RoutedEventArgs e)
    {
        popup.IsOpen = true; // Open the popup
    }
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Johan Smarius 470 Reputation points MVP
    2023-09-03T12:14:10.55+00:00

    In https://learn.microsoft.com/en-us/dotnet/desktop/wpf/windows/how-to-open-window-dialog-box?view=netdesktop-7.0 the process is explained.

    • Create your own window.
    • Set the Owner of the newly created window to the current window
    • Call ShowDialog on the window created in step 1

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.