2013-06-19 118 views
2

我有一个应用程序在第二个屏幕上运行,当用户在第一个屏幕上运行应用程序时,应用程序检测到第二个显示器并将其位置更改为第二个屏幕。wpf窗口位置问题

这有一个问题,主窗口的子项出现在第一个显示器中。如果所有者属性正确建立,则不应该发生这种情况。

Window1 w = new Window1(); 
win.Owner = Application.Current.MainWindow; 

我的应用程序很复杂,它由调用子窗口的组件组成,但我附上了一段代码来说明问题。 。在执行第一个监视器的代码,在窗口中手动移动到辅助监视器,然后按下按钮来调用子窗口这就是出现在第一个显示器:(

注意:我知道,我可以写一个代码,检测各子窗口辅助监视器,并移动到那时,不过我想一个解决方案更加简单,正确,如果有可能

注2:运行Visual Studio的外部应用程序,从“.EXE”直接。在Visual Studio中工作正常。

<Window x:Class="Test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Control="clr-namespace:Borrarrrrr" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Control:UserControl1 x:Name="ctr" /> 
    </Grid> 
</Window> 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Window1 w = new Window1(); 
     //w.Owner = this; 
     w.Owner = Application.Current.MainWindow; 
     w.Show(); 
    } 
} 


<UserControl x:Class="Test.UserControl1" 
      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" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Button Click="Button_Click"></Button> 
    </Grid> 
</UserControl> 

    public partial class UserControl1 : UserControl 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      Window1 w = new Window1(); 
      w.Owner = Application.Current.MainWindow; 
      w.Show(); 
     } 
    } 


<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" 
     WindowStartupLocation="CenterOwner" WindowStyle="None" AllowsTransparency="True" 
     WindowState="Maximized"> 
    <Grid Background="Aqua"> 

    </Grid> 
</Window> 
+0

任何响应??? – Keniako

回答

3

尝试设置Window.WindowStartupLocation

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Window1 w = new Window1(); 
    w.Owner = Application.Current.MainWindow; 
    w.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; 
    w.Show(); 
} 
+0

在一个MDI应用程序,其中不仅主窗口可能会产生新的窗口,它可能更可取用'w.Owner = this;' –

+0

感谢您的回应,显然这是行不通的。中心拥有者的财产已经在XAML中建立。 WindowStartupLocation = “CenterOwner”。我鼓励你使用代码,我认为有一个WPF错误。 – Keniako

+0

设置子窗口的'WindowStartupLocation'后,行为是否有任何改变?设置这对我来说达到了所需的结果。 – StaWho