Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BooleanStringConverter #1823

Merged
merged 4 commits into from Jul 7, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Prev

fix up code

  • Loading branch information...
nopara73 committed Jul 7, 2019
commit cd0f7a4e06364109cc9c008cb41879dc2f17c2ab
@@ -60,8 +60,7 @@
</i:Interaction.Behaviors>
</controls:NoparaPasswordBox>
<DockPanel VerticalAlignment="Top" LastChildFill="True">
<Button Content="{Binding ShowSensitiveKeys, StringFormat=\{0\} Sensitive Keys,
Converter={StaticResource BooleanStringConverter}, ConverterParameter=Hide:Show}"
<Button Content="{Binding ShowSensitiveKeys, StringFormat=\{0\} Sensitive Keys, Converter={StaticResource BooleanStringConverter}, ConverterParameter=Hide:Show}"
Command="{Binding ToggleSensitiveKeysCommand}" DockPanel.Dock="Right" />
<Grid></Grid>
</DockPanel>
@@ -1,4 +1,4 @@
using Avalonia.Data.Converters;
using Avalonia.Data.Converters;
using System;
using System.Globalization;

@@ -8,22 +8,27 @@ public class BooleanStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool on))
if (value is bool on)
{
throw new TypeArgumentException(value, typeof(bool), nameof(value));
}
if (!(parameter is string str))
{
throw new TypeArgumentException(parameter, typeof(string), nameof(parameter));
}
if (parameter is string str)
{
var options = str.Split(':');
if (options.Length < 2)
{
throw new ArgumentException("Two options are required by the converter.", nameof(parameter));
}

var options = str.Split(':');
if (options.Length < 2)
return on ? options[0] : options[1];
}
else
{
throw new TypeArgumentException(parameter, typeof(string), nameof(parameter));
}
}
else
{
throw new ArgumentException("Two options are required by the converter.", nameof(parameter));
throw new TypeArgumentException(value, typeof(bool), nameof(value));
}

return on ? options[0] : options[1];
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -32,17 +37,3 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
}
}
}

namespace WalletWasabi.Gui.Converters
{
public static class BoolConverters
{
public static readonly IValueConverter Not =
new FuncValueConverter<bool, bool>(x => !x);

public static IValueConverter Stringify(string trueString, string falseString)
=> new FuncValueConverter<bool, string>(x => x ? trueString : falseString);

public static readonly IValueConverter OnOff = Stringify("On", "Off");
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.