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
{
// 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 (value == null)
{
return null;
}
return System.Convert.ToDouble(value);
}
@ -22,7 +26,7 @@ public class IntDoubleConverter : IValueConverter
{
if (targetType == typeof(int?))
{
return System.Convert.ToInt64(value);
return System.Convert.ToInt32(value);
}
throw new ArgumentException($"Unsupported type {targetType}");

10
StabilityMatrix/Converters/LaunchOptionConverter.cs

@ -6,7 +6,7 @@ namespace StabilityMatrix.Converters;
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))
{
@ -20,11 +20,19 @@ public class LaunchOptionConverter : IValueConverter
if (targetType == typeof(double?))
{
if (value == null)
{
return null;
}
return double.TryParse(value?.ToString(), out var doubleValue) ? doubleValue : 0;
}
if (targetType == typeof(int?))
{
if (value == null)
{
return null;
}
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
CloseButtonText="Close"
DialogHeight="512"
DialogWidth="640"
Loaded="LaunchOptionsDialog_OnLoaded"
Title="Launch Options"
d:DataContext="{d:DesignInstance Type=viewModels:LaunchOptionsDialogViewModel,
IsDesignTimeCreatable=True}"
d:DesignHeight="512"
d:DesignWidth="512"
DialogHeight="512"
DialogWidth="640"
mc:Ignorable="d"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
@ -27,9 +27,10 @@
<Style BasedOn="{StaticResource {x:Type ui:ContentDialog}}" TargetType="{x:Type local:LaunchOptionsDialog}" />
<converters:LaunchOptionConverter x:Key="LaunchOptionConverter" />
<converters:ValueConverterGroup x:Key="LaunchOptionIntToDoubleConverter">
<converters:LaunchOptionIntDoubleConverter x:Key="LaunchOptionIntDoubleConverter" />
<converters:ValueConverterGroup x:Key="LaunchOptionIntToStringConverter">
<converters:LaunchOptionConverter />
<converters:IntDoubleConverter />
<converters:LaunchOptionIntDoubleConverter />
</converters:ValueConverterGroup>
<!-- Bool type card (checkboxes) -->
@ -43,8 +44,8 @@
FontSize="16"
FontWeight="Bold"
Margin="0,8"
TextWrapping="Wrap"
Text="{Binding Title}" />
Text="{Binding Title}"
TextWrapping="Wrap" />
<StackPanel Orientation="Horizontal">
<ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate>
@ -69,8 +70,8 @@
FontSize="16"
FontWeight="Bold"
Margin="0,8"
TextWrapping="Wrap"
Text="{Binding Title}" />
Text="{Binding Title}"
TextWrapping="Wrap" />
<ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate>
<DataTemplate>
@ -102,8 +103,8 @@
FontSize="16"
FontWeight="Bold"
Margin="0,8"
TextWrapping="Wrap"
Text="{Binding Title}" />
Text="{Binding Title}"
TextWrapping="Wrap" />
<ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate>
<DataTemplate>
@ -112,7 +113,9 @@
<ui:NumberBox
HorizontalAlignment="Stretch"
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" />
</StackPanel>
</DataTemplate>

Loading…
Cancel
Save