WPF Window size have monitor size [duplicate]

雨燕双飞 提交于 2020-01-23 08:00:11

问题


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

The Window's XAML code is this:

<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:

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?


回答1:


use this in code-behind:

a.WindowState = WindowState.Maximized;

and remove SizeToContent from your 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.




回答2:


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

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


来源:https://stackoverflow.com/questions/10701998/wpf-window-size-have-monitor-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!