Browse Source

Fix Numerbox conversion

pull/5/head
Ionite 1 year ago
parent
commit
6781c297f2
No known key found for this signature in database
  1. 10
      StabilityMatrix/Converters/IntDoubleConverter.cs
  2. 10
      StabilityMatrix/Converters/LaunchOptionConverter.cs
  3. 34
      StabilityMatrix/Converters/LaunchOptionIntDoubleConverter.cs
  4. 25
      StabilityMatrix/LaunchOptionsDialog.xaml

10
StabilityMatrix/Converters/IntDoubleConverter.cs

@ -7,10 +7,14 @@ namespace StabilityMatrix.Converters;
public class IntDoubleConverter : IValueConverter public class IntDoubleConverter : IValueConverter
{ {
// Convert from int to double // Convert from int to double
public object Convert(object? value, Type targetType, object parameter, CultureInfo culture) public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
{ {
if (targetType == typeof(double?)) if (targetType == typeof(double?))
{ {
if (value == null)
{
return null;
}
return System.Convert.ToDouble(value); return System.Convert.ToDouble(value);
} }
@ -22,7 +26,7 @@ public class IntDoubleConverter : IValueConverter
{ {
if (targetType == typeof(int?)) if (targetType == typeof(int?))
{ {
return System.Convert.ToInt64(value); return System.Convert.ToInt32(value);
} }
throw new ArgumentException($"Unsupported type {targetType}"); throw new ArgumentException($"Unsupported type {targetType}");

10
StabilityMatrix/Converters/LaunchOptionConverter.cs

@ -6,7 +6,7 @@ namespace StabilityMatrix.Converters;
public class LaunchOptionConverter : IValueConverter public class LaunchOptionConverter : IValueConverter
{ {
public object Convert(object? value, Type targetType, object parameter, CultureInfo culture) public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
{ {
if (targetType == typeof(string)) if (targetType == typeof(string))
{ {
@ -20,11 +20,19 @@ public class LaunchOptionConverter : IValueConverter
if (targetType == typeof(double?)) if (targetType == typeof(double?))
{ {
if (value == null)
{
return null;
}
return double.TryParse(value?.ToString(), out var doubleValue) ? doubleValue : 0; return double.TryParse(value?.ToString(), out var doubleValue) ? doubleValue : 0;
} }
if (targetType == typeof(int?)) if (targetType == typeof(int?))
{ {
if (value == null)
{
return null;
}
return int.TryParse(value?.ToString(), out var intValue) ? intValue : 0; return int.TryParse(value?.ToString(), out var intValue) ? intValue : 0;
} }

34
StabilityMatrix/Converters/LaunchOptionIntDoubleConverter.cs

@ -0,0 +1,34 @@
using System;
using System.Globalization;
using System.Windows.Data;
namespace StabilityMatrix.Converters;
public class LaunchOptionIntDoubleConverter : IValueConverter
{
// Convert from int to double
public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == typeof(double?))
{
if (value == null)
{
return null;
}
return System.Convert.ToDouble(value);
}
throw new ArgumentException($"Unsupported type {targetType}");
}
// Convert from double to object int (floor)
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == typeof(int?) || targetType == typeof(object))
{
return System.Convert.ToInt32(value);
}
throw new ArgumentException($"Unsupported type {targetType}");
}
}

25
StabilityMatrix/LaunchOptionsDialog.xaml

@ -1,13 +1,13 @@
<ui:ContentDialog <ui:ContentDialog
CloseButtonText="Close" CloseButtonText="Close"
DialogHeight="512"
DialogWidth="640"
Loaded="LaunchOptionsDialog_OnLoaded" Loaded="LaunchOptionsDialog_OnLoaded"
Title="Launch Options" Title="Launch Options"
d:DataContext="{d:DesignInstance Type=viewModels:LaunchOptionsDialogViewModel, d:DataContext="{d:DesignInstance Type=viewModels:LaunchOptionsDialogViewModel,
IsDesignTimeCreatable=True}" IsDesignTimeCreatable=True}"
d:DesignHeight="512" d:DesignHeight="512"
d:DesignWidth="512" d:DesignWidth="512"
DialogHeight="512"
DialogWidth="640"
mc:Ignorable="d" mc:Ignorable="d"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}" ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}" ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
@ -27,9 +27,10 @@
<Style BasedOn="{StaticResource {x:Type ui:ContentDialog}}" TargetType="{x:Type local:LaunchOptionsDialog}" /> <Style BasedOn="{StaticResource {x:Type ui:ContentDialog}}" TargetType="{x:Type local:LaunchOptionsDialog}" />
<converters:LaunchOptionConverter x:Key="LaunchOptionConverter" /> <converters:LaunchOptionConverter x:Key="LaunchOptionConverter" />
<converters:ValueConverterGroup x:Key="LaunchOptionIntToDoubleConverter"> <converters:LaunchOptionIntDoubleConverter x:Key="LaunchOptionIntDoubleConverter" />
<converters:ValueConverterGroup x:Key="LaunchOptionIntToStringConverter">
<converters:LaunchOptionConverter /> <converters:LaunchOptionConverter />
<converters:IntDoubleConverter /> <converters:LaunchOptionIntDoubleConverter />
</converters:ValueConverterGroup> </converters:ValueConverterGroup>
<!-- Bool type card (checkboxes) --> <!-- Bool type card (checkboxes) -->
@ -43,8 +44,8 @@
FontSize="16" FontSize="16"
FontWeight="Bold" FontWeight="Bold"
Margin="0,8" Margin="0,8"
TextWrapping="Wrap" Text="{Binding Title}"
Text="{Binding Title}" /> TextWrapping="Wrap" />
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<ItemsControl ItemsSource="{Binding Options}"> <ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate> <ItemsControl.ItemTemplate>
@ -69,8 +70,8 @@
FontSize="16" FontSize="16"
FontWeight="Bold" FontWeight="Bold"
Margin="0,8" Margin="0,8"
TextWrapping="Wrap" Text="{Binding Title}"
Text="{Binding Title}" /> TextWrapping="Wrap" />
<ItemsControl ItemsSource="{Binding Options}"> <ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate> <ItemsControl.ItemTemplate>
<DataTemplate> <DataTemplate>
@ -102,8 +103,8 @@
FontSize="16" FontSize="16"
FontWeight="Bold" FontWeight="Bold"
Margin="0,8" Margin="0,8"
TextWrapping="Wrap" Text="{Binding Title}"
Text="{Binding Title}" /> TextWrapping="Wrap" />
<ItemsControl ItemsSource="{Binding Options}"> <ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate> <ItemsControl.ItemTemplate>
<DataTemplate> <DataTemplate>
@ -112,7 +113,9 @@
<ui:NumberBox <ui:NumberBox
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
Margin="8" Margin="8"
Value="{Binding OptionValue, Converter={StaticResource LaunchOptionIntToDoubleConverter}}" PlaceholderText="{Binding DefaultValue, Mode=OneWay, Converter={StaticResource LaunchOptionConverter}}"
ValidationMode="InvalidInputOverwritten"
Value="{Binding OptionValue, Converter={StaticResource LaunchOptionIntDoubleConverter}, Mode=TwoWay}"
VerticalAlignment="Stretch" /> VerticalAlignment="Stretch" />
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>

Loading…
Cancel
Save