Resize WPF Window and contents depening on screen resolution
I have a WPF app with multiple controls on each window, some overlayed etc, what i need is a way of getting the app to resize itself automatically depending on the screen resolution.
Any ideas ?
Solution 1:
The syntax Height="{Binding SystemParameters.PrimaryScreenHeight}" provides the clue but doesn't work as such. SystemParameters.PrimaryScreenHeight is static, hence you shall use:
<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"
>
And it would fit the whole screen. Yet, you may prefer to fit a percentage of the screen size, e.g. 90%, in which case the syntax must be amended with a converter in a binding spec:
<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"
>
Where RatioConverter is here declared in MyApp.Tools namespace as follows:
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());
}
}
}
Where the definition of the converter shall inherit from MarkupExtension in order to be used directly in the root element without a former declaration as a resource.
Solution 2:
Just simply make a binding like that:
<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}">
Solution 3:
Based on berhauz's answer. But you can simplify it even further by doing it in code behind(.xaml.cs):
public Window1()
{
InitializeComponent();
this.Height = SystemParameters.PrimaryScreenHeight * 0.95;
this.Width = SystemParameters.PrimaryScreenWidth * 0.95;
}