Location of UserControl x:Class="LatestAlarm3.MessageBoxWpf" when started

I want to have control of where MessageBoxWpf is displayed.

Center of screen would be adequate.

The problem that it always displays in the upper-left.

<UserControl x:Class="LatestAlarm3.MessageBoxWpf"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:LatestAlarm3"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Popup Name="myPopup" IsOpen="False" HorizontalAlignment="Center" VerticalAlignment="Center" PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}">
        <Border BorderThickness="1">
            <StackPanel>
                <TextBlock Name="myPopupText" Background="LightBlue" Foreground="Blue" Padding="30">
                Popup Text
                </TextBlock>
                <StackPanel Orientation="Horizontal">
                    <Button Click="ButtonOK_Click">OK</Button>
                    <Button x:Name="myPopupCancelButton" Click="ButtonCancel_Click">Cancel</Button>
                </StackPanel>
            </StackPanel>
        </Border>
    </Popup>
</UserControl>
 
public static LatestAlarm3.DialogResult Show(string text, string caption)
{
    _messageBoxWpf = new MessageBoxWpf();
 
    //this.Owner = Application.Current.MainWindow;
    //this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
    //_messageBoxWpf.myPopup.PlacementTarget = uIElement; 
    _messageBoxWpf.myPopupText.Text = text;
    _messageBoxWpf.myPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Center;
    _messageBoxWpf.myPopupCancelButton.Visibility = Visibility.Collapsed;
    _messageBoxWpf.myPopup.IsOpen = true;
    return _dialogResult;
}

Hi @David Gradwell

Looking at the code, I think it’s because of the following snippet //this.WindowStartupLocation = WindowStartupLocation.CenterOwner;

One solution is to set the PlacementTarget to the main window in code-behind explicitly or to use a proper Window with WindowStartupLocation = WindowStartupLocation.CenterScreen instead of WindowStartupLocation.CenterOwner

Here’s an example

<!--Popup-->
<Window
    x:Class="WpfApp1.MessageBoxWpf"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="NoResize"
    ShowInTaskbar="False"
    SizeToContent="WidthAndHeight"
    WindowStartupLocation="CenterOwner"
    WindowStyle="None"
    mc:Ignorable="d">
    <Border
        Padding="10"
        Background="White"
        BorderBrush="#007ACC"
        BorderThickness="1"
        CornerRadius="5">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
 
            <TextBlock
                x:Name="TitleTextBlock"
                Grid.Row="0"
                Margin="0,0,0,10"
                FontWeight="Bold" />
 
            <TextBlock
                x:Name="MessageTextBlock"
                Grid.Row="1"
                Margin="0,0,0,20"
                TextWrapping="Wrap" />
 
            <StackPanel
                Grid.Row="2"
                HorizontalAlignment="Right"
                Orientation="Horizontal">
                <Button
                    x:Name="OkButton"
                    Width="75"
                    Height="25"
                    Click="OkButton_Click"
                    Content="OK" />
            </StackPanel>
        </Grid>
    </Border>
</Window>
 
using System;
using System.Windows;
 
namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MessageBoxWpf.xaml
    /// </summary>
    public partial class MessageBoxWpf : Window
    {
        public enum DialogResult
        {
            OK,
            None
        }
 
        private static DialogResult _dialogResult = DialogResult.None;
 
        public MessageBoxWpf()
        {
            InitializeComponent();
        }
 
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            _dialogResult = DialogResult.OK;
            this.Close();
        }
 
        /// <summary>
        /// Shows a message box with OK button
        /// </summary>
        /// <param name="text">The message to display</param>
        /// <param name="caption">The title of the message box</param>
        /// <returns>DialogResult indicating user's choice</returns>
        public static DialogResult Show(string text, string caption)
        {
            _dialogResult = DialogResult.None;
            
            var messageBox = new MessageBoxWpf();
            messageBox.TitleTextBlock.Text = caption;
            messageBox.MessageTextBlock.Text = text;
            
            // Center in the screen, regardless of owner
            messageBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            
            // Show modal dialog
            messageBox.ShowDialog();
            
            return _dialogResult;
        }
    }
}
 
<!-- Main Window Event -->
 
 private void ShowMessageBoxButton_Click(object sender, RoutedEventArgs e)
 {
     var result = MessageBoxWpf.Show("This is a test message.", "Information");
     
     if (result == MessageBoxWpf.DialogResult.OK)
     {
         // You can handle the result if needed
         Title = "User clicked OK";
     }
 }

Is this a Smart Client (SC) plugin?

Does the popup work for you if you test it in your own WPF app, which isn’t SC?

Does the popup appear upper left on the SC window or on the screen? (If not as simple as that please include a screen capture.)

Where and how do you open the popup in the SC plugin?

Bo. Thanks for following up with this.

YES, this a Smart Client (SC) plug-in.

I’m no longer using UserControl or Popup.

The problem was that “UserControl” was the only WPF option available when adding a control in VisualStudio in my (SC) project.

So I found a way to create a “Window” and copied it into my SC project.
Now this issue is SOLVED now. Thanks.

I’m an experienced programmer, but WPF is new to me, and did not know the difference between a “Window” and a “UserControl”.

Important WPF Question, please read …

I have heard that soon, the (SC) will not support Forms and we need to use WPF.

I have done that in this plug-in, but there remain a few references to System.Windows.Forms.

Do I need to change anything like in the following example?

For example, the UserControl in the PluginDefinition.

public class LatestAlarm3Definition : PluginDefinition

{

///

/// A user control to display when the administrator clicks on the top TreeNode

///

public override UserControl GenerateUserControl()

{

//2025-06-24 _treeNodeInofUserControl = new HelpPage();

return null; //2025-06-24 _treeNodeInofUserControl;

}

)

David Gradwell

App-Techs Corporation

The forms in the Management Client (Admin folder in a plugin project) are still Forms.