如何将WPF窗口大小设置为相对于显示器屏幕的25%?

9

我有一个WPF窗口,如何设置WPF窗口大小为相对于显示器屏幕的25%?如何设置这些属性。


这个答案会帮助你入门,然后你就可以做数学来调整窗口大小。https://dev59.com/WXI-5IYBdhLWcg3wSGUB#2118993 - DLeh
2个回答

29
在您的MainWindow构造函数中添加。
this.Height = (System.Windows.SystemParameters.PrimaryScreenHeight * 0.25);
this.Width = (System.Windows.SystemParameters.PrimaryScreenWidth * 0.25);

另外不要在你的MainWindows.xaml中设置WindowState="Maximized",否则它将不起作用。 希望这可以帮到你。


4
使用XAML绑定,或者在MainWindow.xaml中进行操作。
<Window x:Class="App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Main Window" 
        Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource WindowSizeConverter}, ConverterParameter='0.6'}" 
        Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={StaticResource WindowSizeConverter}, ConverterParameter='0.8'}" >

Resources.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:App">
    <local:WindowSizeConverter x:Key="WindowSizeConverter"/>
</ResourceDictionary>

WindowSizeConverter.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace App
{
    public class WindowSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double size = System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
            return size.ToString("G0", CultureInfo.InvariantCulture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => DependencyProperty.UnsetValue;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接