2

I have two monitors. When my application runs, the parent is displayed on the first monitor. When I move the parent window to the second monitor and click a button (diaplay a xaml window for loading), this child window stays on the first monitor. Is there a way to make the child window stay with the parent window no matter where the parent window is located?

please note: parent is winform ... child is xaml.

loading xaml (child)

<Window x:Class="Test.Loading"
    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"
    WindowStyle="None" 
    AllowsTransparency="True" 
    WindowState="Maximized"
    Background="Gray">

    <Grid>
        <Border>
            <TextBlock Text="Loading ..." />
        </Border>
    </Grid>
</Window>

Parent

    private void btnShowLoading_Click(object sender, EventArgs e)
{
    Loading load = new Loading();

    double top = this.Top;
    double left = this.Left;

    load.Top = top;
    load.Left = left;

    load.Show();
}
user1884032
  • 337
  • 1
  • 5
  • 18

2 Answers2

0

Try this:

childWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;

UPDATE 1:

Get your parent window's location and set it to child window. If you think your parent window's state changes, you may use this to get actual Top and Left:

var leftField = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var topField = typeof(Window).GetField("_actualTop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

double left = (double)leftField.GetValue(parentWindow);

double top = (double)topField.GetValue(parentWindow);

source: Window ActualTop, ActualLeft

UPDATE 2: First of all you had to tell that the parentWindow is WinForms. In this case you can get parentWindow's Top and Left like this:

double top = this.Top;
double left = this.Left;

Then pass the top and left of the parentWindow to the childWindow. When childWindow loaded it should set top and left.

Community
  • 1
  • 1
Dilshod
  • 3,189
  • 3
  • 36
  • 67
0

In WPF you need to manually set the owner window - it is not done automatically. From Window.Show:

A window that is opened by calling Show does not automatically have a relationship with the window that opened it; specifically, the opened window does not know which window opened it. This relationship can be established using the Owner property and managed using the OwnedWindows property.

Since your Loading window has no owner, it will be centered on your primary monitor. Use the code below:

private void btnShowLoading_Click(object sender, EventArgs e)
{
    Loading load = new Loading();
    load.Owner = this;
    load.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; 
    load.Show();
}
shf301
  • 31,086
  • 2
  • 52
  • 86
  • my parent window is a winform control while the child is a xaml(window) control. I dont know how to set the owner of the child window. When I set the owner to the parent window, i get error of "Cannot implicitly convert type system.windows.forms.form to system.windows.window. – user1884032 Mar 21 '13 at 17:12
  • Are you hosting the winform control in a `WindowsFormsHost`? If so you would have to get a reference to it. If not how are you combining WinForms and WPF? – shf301 Mar 21 '13 at 18:29
  • no, i have a winform which gets access to the xaml by namespace. – user1884032 Mar 21 '13 at 18:34