2011-11-14 36 views
13

我有一个WPF应用程序与每个窗口上的多个控件,一些覆盖等,我需要的是一种让应用程序根据屏幕分辨率自动调整自己的方式。调整WPF窗口和内容依赖于屏幕分辨率

任何想法?

+0

为什么你需要这个?如果你想简单地调整窗口大小,@Fischermaen给了你答案。但是如果你想改变字体的大小等,这是不需要的,因为WPF已经管理这个。所有WPF渲染都在虚拟坐标中工作,虚拟坐标根据系统的DPI设置映射到物理像素。 –

+0

你想让你的窗户的尺寸与其内容要求的尺寸完全相同吗?然后阅读这个类似的[问题](http://stackoverflow.com/questions/1746431/wpf-control-size-to-content)。 – dowhilefor

+1

但我的wpf有坐标集和字体大小设置等基本上软件需要适用于不同的分辨率 –

回答

19

只需简单地作出这样一个结合:

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

这会调整窗体上的所有控件的大小吗? –

+0

这取决于你如何设计你的表单。您可以拥有修复设计,或者控件将相对于其容器调整大小。 – Fischermaen

+0

哦有趣,我希望我知道,当我开始时,哦,好生病给它一个空白项目的测试,看看会发生什么。所以为了澄清,需要使控制项目的宽度和高度相对?我有这个 ,我将如何使这个窗口调整大小? –

42

语法身高=“{结合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继承,以便直接在根元素中使用,而不需要以前的声明作为资源。

+2

希望我能upvote不止一次。 – phonetagger

+0

如何以编程方式设置'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'}“' –

+0

我同意phonetagger这是一个很好的答案,只需使它适合屏幕尺寸的一个百分比,这样无论分辨率/显示器尺寸如何,它都会始终适合... – AndyUK