根据屏幕分辨率调整 WPF 窗口和内容的大小

问题描述 投票:0回答:5

我有一个 WPF 应用程序,每个窗口上都有多个控件,有些是重叠的等,我需要的是一种让应用程序根据屏幕分辨率自动调整自身大小的方法。

有什么想法吗?

c# wpf resize
5个回答
96
投票

语法 Height="{Binding SystemParameters.PrimaryScreenHeight}" 提供了线索,但不起作用。 SystemParameters.PrimaryScreenHeight 是静态的,因此您应该使用:

  <Window x:Class="MyApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:tools="clr-namespace:MyApp.Tools"
      Height="{x:Static SystemParameters.PrimaryScreenHeight}" 
      Width="{x:Static SystemParameters.PrimaryScreenWidth}" 
      Title="{Binding Path=DisplayName}"
      WindowStartupLocation="CenterScreen"
      Icon="icon.ico"
  >

它会适合整个屏幕。然而,您可能更喜欢适应屏幕尺寸的百分比,例如90%,在这种情况下,必须使用绑定规范中的转换器修改语法:

  <Window x:Class="MyApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:tools="clr-namespace:MyApp.Tools"
      Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={tools:RatioConverter}, ConverterParameter='0.9' }" 
      Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={tools:RatioConverter}, ConverterParameter='0.9' }" 
      Title="{Binding Path=DisplayName}"
      WindowStartupLocation="CenterScreen"
      Icon="icon.ico"
  >

RatioConverter 在 MyApp.Tools 命名空间中声明如下:

namespace MyApp.Tools {

    [ValueConversion(typeof(string), typeof(string))]
    public class RatioConverter : MarkupExtension, IValueConverter
    {
      private static RatioConverter _instance;

      public RatioConverter() { }

      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      { // do not let the culture default to local to prevent variable outcome re decimal syntax
        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)
      { // read only converter...
        throw new NotImplementedException();
      }

      public override object ProvideValue(IServiceProvider serviceProvider)
      {
        return _instance ?? (_instance = new RatioConverter());
      }

    }
}

转换器的定义应继承自 MarkupExtension,以便直接在根元素中使用,而无需先前声明为资源。


34
投票

只需简单地进行这样的绑定即可:

<Window x:Class="YourApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="YourApplication" 
    Height="{Binding SystemParameters.PrimaryScreenHeight}" 
    Width="{Binding SystemParameters.PrimaryScreenWidth}">

1
投票

基于伯豪兹的回答。但是您可以通过在代码后面(.xaml.cs)中进行进一步简化:

public Window1()
{
    InitializeComponent();
    this.Height = SystemParameters.PrimaryScreenHeight * 0.95;
    this.Width = SystemParameters.PrimaryScreenWidth * 0.95;
}

0
投票

@berhauz 答案的略微改进版本。使用现代模式匹配并省略字符串到双精度到字符串的转换:

[ValueConversion(typeof(double), typeof(double))]
public class RatioConverter : MarkupExtension, IValueConverter
{
  private static RatioConverter _instance;

  public RatioConverter() { }

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    if (value == null || parameter is null)
      return null;
    else if (value is double v && parameter is double p)
      return v * p;
    else
      throw new ArgumentException("Both value and parameter must be of type double.");
}

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  { 
    throw new NotSupportedException();
  }

  public override object ProvideValue(IServiceProvider serviceProvider)
  {
    return _instance ?? (_instance = new RatioConverter());
  }
}

像这样使用它:

<Window.Width>
  <Binding Source="{x:Static SystemParameters.PrimaryScreenWidth}" Converter="{local:RatioConverter}">
    <Binding.ConverterParameter>
      <sys:Double>0.9</sys:Double>
    </Binding.ConverterParameter>
  </Binding>
</Window.MaxWidth>

0
投票

将游览主窗口的“WindowState”属性设置为“Maximized”: WindowState=“最大化”

© www.soinside.com 2019 - 2024. All rights reserved.