0

I have a WPF App and i want to keep all the objects(textboxes,labels and datagrid) within scale/ratio. So i created a class within the app to code a ratio converter as seen below (Not MVVM, NO MODEL CLASSES)

 namespace WpfApp2
 {
[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());
    }

}

}

Here's the code for the XAML Window where i configured it

< Window x:Class = "WpfApp2.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:WpfApp2"

    xmlns:tools="clr-namespace:WpfApp2"

    mc:Ignorable="d"

     
   Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={local:RatioConverter}, ConverterParameter='0.8' }"   Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={tools:RatioConverter}, ConverterParameter='0.8' }"  WindowStartupLocation="CenterScreen"   Title="MainWindow"    Background="Aqua">

It doesnt appear to be working. Am I missing something? Is there something not properly configured?

  • 2
    https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.viewbox?view=net-5.0 – aybe Jun 16 '21 at 23:13
  • We once working on an enterprise application have created custom scaling logic that scales based on the resolution, window size (width and height) and layout percentage. But unfortunately, I cannot share that code as that is USP for them. But here is the link that has something close to what we have done https://stackoverflow.com/a/5000120/1966993 – G K Jun 17 '21 at 07:00

0 Answers0