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?