我有一个WPF应用程序,每个窗口上都有多个控件,一些控件会重叠等等,我需要的是一种根据屏幕分辨率自动调整应用程序大小的方法。
有什么想法吗?
我有一个WPF应用程序,每个窗口上都有多个控件,一些控件会重叠等等,我需要的是一种根据屏幕分辨率自动调整应用程序大小的方法。
有什么想法吗?
语法 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' }"
- Vibhesh Kaul简单地创建一个绑定,就像这样:
<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}">
<Grid> <Rectangle HorizontalAlignment="Stretch" Margin="110,48,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Stretch" Fill="#FFD43939" /> </Grid>
- Fischermaen基于berhauz的答案。但你甚至可以通过在代码后端(.xaml.cs)执行来进一步简化它:
public Window1()
{
InitializeComponent();
this.Height = SystemParameters.PrimaryScreenHeight * 0.95;
this.Width = SystemParameters.PrimaryScreenWidth * 0.95;
}
稍微改进了@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>