【问题标题】:Resize WPF Window and contents depening on screen resolution根据屏幕分辨率调整 WPF 窗口和内容的大小
【发布时间】:2011-12-28 15:28:48
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 为什么需要这个?如果您想简单地调整窗口大小,@Fischermaen 给了您答案。但是如果你想改变字体的大小等。这不是必需的,因为 WPF 已经管理了这个。所有 WPF 渲染都在虚拟坐标中工作,根据系统的 DPI 设置映射到物理像素。
  • 您希望您的窗口完全符合其内容所需的大小吗?然后阅读类似的question
  • 但我的 wpf 设置了坐标和字体大小等。基本上该软件需要适用于不同的分辨率
  • 威尔士你有没有找到解决方案? @Fischermaens 解决方案不起作用
  • 这与最大化窗口有何不同?

标签: c# wpf resize


【解决方案1】:

语法 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 继承,以便直接在根元素中使用,而无需先前声明为资源。

【讨论】:

  • 如何以编程方式设置这个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' }"
  • 我同意 phonetagger 这是一个很好的答案,只需让它适合屏幕尺寸的百分比,这样无论分辨率/显示器尺寸如何,它总是适合...跨度>
  • 如果不想让窗口覆盖任务栏,可以使用MaximizedPrimaryScreenHeight代替PrimaryScreenHeight。 (stackoverflow.com/a/35010001)
【解决方案2】:

只需像这样进行绑定:

<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}">

【讨论】:

  • 这会相应地调整表单上所有控件的大小吗?
  • 这取决于您如何设计表单。您可以进行修复设计,否则控件将相对于其容器调整大小。
  • ooh 很有趣,我希望 id 知道当我开始的时候,我会在一个空白项目上对其进行测试,看看会发生什么。所以只是为了澄清,需要控制项目的宽度和高度是相对的吗?我有这个 ,我如何使这个调整大小与窗口一致?
  • @WelshKing:你必须像这样声明你的网格` `
  • 可爱的作品是一种享受,现在可以玩多个控件层:) 谢谢
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多