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,以便直接在根元素中使用,而不需要将前一个声明作为资源.
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)