//得到屏幕工作区域宽度
double x = SystemParameters.WorkArea.Width;
//得到屏幕工作区域高度
double y = SystemParameters.WorkArea.Height;
//得到屏幕整体宽度
double x1= SystemParameters.PrimaryScreenWidth;
//得到屏幕整体高度
double y1 = SystemParameters.PrimaryScreenHeight;
我喜欢的 WPF Window 大小设置:
Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource RatioConverter}, ConverterParameter='0.8'}"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Height, Converter={StaticResource RatioConverter}, ConverterParameter='1.6'}"
MinHeight="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource RatioConverter}, ConverterParameter='0.5'}"
MinWidth="{Binding RelativeSource={RelativeSource Self}, Path=MinHeight, Converter={StaticResource RatioConverter}, ConverterParameter='1.6'}"
其中 RatioConverter 的实现代码如下:
[ValueConversion(typeof(string), typeof(string))]
public class RatioConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var size = 0d;
if (value != null)
size = System.Convert.ToDouble(value, CultureInfo.InvariantCulture) *
System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
return size;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}