C# WPF 窗口大小有监视器大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10701998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:53:41  来源:igfitidea点击:

WPF Window size have monitor size

c#wpfwindowmonitor

提问by Ionic? Biz?u

I am new in WPF and I want to make a window that has max sizes (monitor sizes).

我是 WPF 的新手,我想制作一个具有最大尺寸(监视器尺寸)的窗口。

The Window's XAML code is this:

Window 的 XAML 代码是这样的:

<Window x:Class="WpfApplication1.Stop"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Stop" Height="{Binding}" Width="{Binding}" Background="Black" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="260" d:DesignWidth="348" SizeToContent="WidthAndHeight">
    <Grid>
        <Button Content="Ok" Height="25" HorizontalAlignment="Left" Margin="194,36,0,0" Name="button1" VerticalAlignment="Top" Width="38" Click="button1_Click" />
        <TextBlock Height="17" HorizontalAlignment="Left" Margin="18,16,0,0" Name="textBlock1" Text="Introduceti parola:" VerticalAlignment="Top" Width="246" FontSize="14" Foreground="White" />
        <PasswordBox Height="25" HorizontalAlignment="Left" Margin="12,36,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="176" />
    </Grid>
</Window>

The C# code that open this window is this:

打开这个窗口的 C# 代码是这样的:

Stop a = new Stop();                   
a.Show();
a.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
a.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

How can I set window size to monitor size?

如何设置窗口大小以监视大小?

采纳答案by webber2k6

use this in code-behind:

在代码隐藏中使用它:

a.WindowState = WindowState.Maximized;

and remove SizeToContentfrom your XAML.

SizeToContent从您的XAML.

With this code:

使用此代码:

a.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
a.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

you will have problems when using secondary screen with different resolution.

使用不同分辨率的辅助屏幕时会遇到问题。

回答by user4193339

above comment is working. i did below:- written in mainwindow.xaml.cs:-

以上评论有效。我在下面做了:- 写在 mainwindow.xaml.cs 中:-

public MainWindow()
        {
            this.WindowState = WindowState.Maximized;
            InitializeComponent();
        }