Resize WPF Window and contents depening on screen resolution

前端 未结 2 2041
故里飘歌
故里飘歌 2020-12-01 02:31

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 re

相关标签:
2条回答
  • 2020-12-01 02:56

    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}">
    
    0 讨论(0)
  • 2020-12-01 03:04

    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.

    0 讨论(0)
提交回复
热议问题