【问题标题】:Problems with wpf windows locationwpf windows位置问题
【发布时间】:2013-06-19 16:05:11
【问题描述】:

我有一个在第二个屏幕上运行的应用程序,当用户在第一个屏幕上运行该应用程序时,应用程序会检测到第二个监视器并将其位置更改为辅助屏幕。

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

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

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

注意:我知道我可以编写一个代码来检测每个子窗口中的辅助监视器并移动到那里,但如果可能的话,我想要一个更简单和正确的解决方案。

Note2:直接从“.exe”在 Visual Studio 外部运行应用程序。在 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>

【问题讨论】:

    标签: c# .net wpf windows xaml


    【解决方案1】:

    尝试设置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();
    }
    

    【讨论】:

    • 在 MDI 应用程序中,不仅主窗口可能产生新窗口,最好使用w.Owner = this;
    • 感谢您的回复,显然这不起作用。 centerowner 属性已在 XAML 中建立。 WindowStartupLocation="CenterOwner"。我鼓励你玩代码,我认为有一个 WPF 错误。
    • 子窗口设置WindowStartupLocation后行为有什么变化吗?设置它可以达到我所需的结果。
    • 不适合我。从 Visual Studio 执行应用程序的行为是正确的。子窗口未设置为最大化时也是正确的……但是附上的代码肯定是不好的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多