【问题标题】:How can I set WPF window size is 25 percent of relative monitor screen如何将 WPF 窗口大小设置为相对监视器屏幕的 25%
【发布时间】:2014-10-15 02:55:51
【问题描述】:

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

【问题讨论】:

标签: wpf xaml c#-4.0 desktop-application


【解决方案1】:

在您的 MainWindow 构造函数中添加

this.Height = (System.Windows.SystemParameters.PrimaryScreenHeight * 0.25);
this.Width = (System.Windows.SystemParameters.PrimaryScreenWidth * 0.25);

也不要在 MainWindows.xaml 中设置 WindowState="Maximized" 否则它将不起作用。 希望这会有所帮助。

【讨论】:

    【解决方案2】:

    或者使用 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-18
      • 2012-05-28
      • 2010-10-17
      • 2022-10-06
      • 2014-03-11
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多