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

Wel*_*ing 20 c# wpf resize

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

有任何想法吗 ?

ber*_*auz 74

语法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"
  >
Run Code Online (Sandbox Code Playgroud)

它适合整个屏幕.但是,您可能更喜欢适应屏幕大小的百分比,例如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"
  >
Run Code Online (Sandbox Code Playgroud)

其中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());
      }

    }
}
Run Code Online (Sandbox Code Playgroud)

转换器的定义应继承自MarkupExtension,以便直接在根元素中使用,而不需要将前一个声明作为资源.

  • 如果您不希望窗口覆盖任务栏,可以使用MaximizedPrimaryScreenHeight而不是PrimaryScreenHeight.(/sf/answers/2450700101/) (4认同)
  • 希望我能不止一次地投票. (3认同)

Fis*_*aen 27

只需简单地制作一个绑定:

<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}">
Run Code Online (Sandbox Code Playgroud)

  • @WelshKing:你必须声明你的网格``Grid> <Rectangle Horizo​​ntalAlignment ="Stretch"Margin ="110,48,0,0"Name ="rectangle1"Stroke ="Black"VerticalAlignment ="Stretch"Fill = "#FFD43939"/> </ Grid>` (2认同)