Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何获取所属窗口的大小?_Wpf_Xaml - Fatal编程技术网

Wpf 如何获取所属窗口的大小?

Wpf 如何获取所属窗口的大小?,wpf,xaml,Wpf,Xaml,我想在XAML中将窗口的宽度和高度设置为其所有者窗口的宽度和高度。我该怎么做?如果所有者窗口是主窗口,是否有帮助 <Window Width="???" Height="???"/> 如果要从主窗口创建测试窗口,这显然是我认为您需要的代码。您可以使用this.width和this.height关键字直接设置它的宽度和高度 var window = new TestWindow(); window.Width = this.Width; window.Height = this.He

我想在XAML中将窗口的宽度和高度设置为其所有者窗口的宽度和高度。我该怎么做?如果所有者窗口是主窗口,是否有帮助

<Window Width="???" Height="???"/>

如果要从主窗口创建测试窗口,这显然是我认为您需要的代码。您可以使用
this.width
this.height
关键字直接设置它的宽度和高度

var window = new TestWindow();
window.Width = this.Width;
window.Height = this.Height;
window.Show();
如果要从其构造函数设置TestWindow宽度和高度,请在TestWindow构造函数中执行以下操作:

foreach(var window in Application.Current.Windows)
{
    if (window is MainWindow)
    {
            this.Width = ((Window)window).Width;
            this.Height = ((Window)window).Height;

    }
}

嘿,你可以通过XAML来实现,就像:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ListBoxTest" x:Class="ListBoxTest.ChildWindows"
        Title="ChildWindows" Height="{Binding ActualWidth, Mode=OneWay}" Width="{Binding ActualWidth, Mode=OneWay}">
    <Window.DataContext>
        <local:MainWindow/>
    </Window.DataContext>
    <Grid>
        <!--here whatever you want to do...-->
    </Grid>
</Window>

您必须将高度和宽度与主窗口的实际宽度和高度绑定,但在此之前,您必须为作为主窗口的ChildWindws设置DataContext,如:

<Window.DataContext>
    <local:MainWindow/>
</Window.DataContext>


这里的父窗口是主窗口。

Window.Size=新大小(宽度、高度)@安东:什么是宽度和高度?那XAML是怎样的呢?你需要在XAML或.cs中设置大小在哪里?
@Anton:我想在XAML中将窗口的宽度和高度设置为其所有者的宽度和高度。你不能从XAML绑定到父窗口。不幸的是,我已经在使用不同的DataContext,因为我正在使用MVVM。使用数据绑定你能详细说明一下吗?将它绑定到一些数据库吗属性使用双向绑定,它应该做到这一点
Width=“{Binding Path=xExt,Mode=TwoWay}”
好的,所以没有内部机制。谢谢