Skip to content

Commit

Permalink
Turn converters into MarkupExtensions for simpler use in xaml
Browse files Browse the repository at this point in the history
  • Loading branch information
astenlund committed Oct 2, 2020
1 parent 6d3f8b5 commit b006879
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using System.Windows.Markup;

namespace fs2ff.Converters
{
public class BooleanToIntConverter : IValueConverter
public class BooleanToDoubleConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool val))
throw new InvalidOperationException($"{nameof(value)} must be a {nameof(Boolean)}");

var ints = parameter.ToString()?.Split(',').Select(int.Parse).ToArray() ?? new int[0];
var doubles = parameter.ToString()?.Split(',').Select(double.Parse).ToArray() ?? new double[0];

if (ints.Length != 2)
if (doubles.Length != 2)
throw new InvalidOperationException($"{nameof(parameter)} must be two comma-separated integer values");

return val
? ints[0]
: ints[1];
? doubles[0]
: doubles[1];
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
8 changes: 7 additions & 1 deletion Converters/BooleanToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace fs2ff.Converters
{
public class BooleanToVisibilityConverter : IValueConverter
public class BooleanToVisibilityConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Expand All @@ -22,5 +23,10 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
? vis == Visibility.Visible
: Binding.DoNothing;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
8 changes: 7 additions & 1 deletion Converters/FormatStringConverter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace fs2ff.Converters
{
public class FormatStringConverter : IValueConverter
public class FormatStringConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object? parameter, CultureInfo culture)
{
Expand All @@ -17,5 +18,10 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
{
throw new NotImplementedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
10 changes: 8 additions & 2 deletions Converters/InvertedBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace fs2ff.Converters
{
public class InvertedBooleanConverter : IValueConverter
public class InvertedBooleanConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Expand All @@ -15,5 +16,10 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
{
return !(bool)value;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
}
8 changes: 7 additions & 1 deletion Converters/IpAddressToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Data;
using System.Windows.Markup;

namespace fs2ff.Converters
{
public class IpAddressToStringConverter : IValueConverter
public class IpAddressToStringConverter : MarkupExtension, IValueConverter
{
private readonly Regex _regex = new Regex(@"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");

Expand Down Expand Up @@ -38,5 +39,10 @@ public object Convert(object? value, Type targetType, object parameter, CultureI
? result
: Binding.DoNothing;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
8 changes: 7 additions & 1 deletion Converters/UIntToDoubleConverter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace fs2ff.Converters
{
public class UIntToDoubleConverter : IValueConverter
public class UIntToDoubleConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Expand All @@ -19,5 +20,10 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
? (int)Math.Round(d)
: Binding.DoNothing;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
2 changes: 1 addition & 1 deletion MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public bool DataTrafficEnabled
if (!Equals(value, _ipAddress))
{
_ipAddress = value;
ResetNetwork();
Preferences.Default.ip_address = value?.ToString() ?? "";
Preferences.Default.Save();
ResetNetwork();
}
}
}
Expand Down
34 changes: 14 additions & 20 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:fs2ff.Behaviors"
xmlns:converters="clr-namespace:fs2ff.Converters"
xmlns:c="clr-namespace:fs2ff.Converters"
ui:WindowHelper.UseModernWindowStyle="True"
mc:Ignorable="d"
Title="{Binding WindowTitle}"
Expand All @@ -16,12 +16,6 @@
Closing="Window_Closing"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<converters:BooleanToIntConverter x:Key="BooleanToIntConverter" />
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:FormatStringConverter x:Key="FormatStringConverter" />
<converters:InvertedBooleanConverter x:Key="InvertedBooleanConverter" />
<converters:IpAddressToStringConverter x:Key="IpAddressToStringConverter" />
<converters:UIntToDoubleConverter x:Key="UIntToDoubleConverter" />
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
Expand Down Expand Up @@ -91,10 +85,10 @@
<TextBox
IsEnabled="{
Binding IsChecked, ElementName=BroadcastCheckBox,
Converter={StaticResource InvertedBooleanConverter}}"
Converter={c:InvertedBooleanConverter}}"
Text="{
Binding IpAddress, Mode=TwoWay,
Converter={StaticResource IpAddressToStringConverter},
Converter={c:IpAddressToStringConverter},
UpdateSourceTrigger=LostFocus}"
Template="{StaticResource WatermarkTextBoxTemplate}"
Tag="255.255.255.255"
Expand All @@ -116,14 +110,14 @@
IsChecked="{Binding DataAttitudeEnabled, Mode=TwoWay}"
Content="Attitude" />
<Label
Content="{Binding Value, ElementName=Slider, Converter={StaticResource FormatStringConverter}, ConverterParameter='ATTITUDE FREQ: {0} Hz'}"
Content="{Binding Value, ElementName=Slider, Converter={c:FormatStringConverter}, ConverterParameter='ATTITUDE FREQ: {0} Hz'}"
Margin="0,14,0,5" />
<Slider
Name="Slider"
IsEnabled="{Binding DataAttitudeEnabled}"
Value="{Binding AttitudeFrequency, Mode=TwoWay, Delay=2000, Converter={StaticResource UIntToDoubleConverter}}"
Minimum="{Binding AttitudeFrequencyMin, Converter={StaticResource UIntToDoubleConverter}}"
Maximum="{Binding AttitudeFrequencyMax, Converter={StaticResource UIntToDoubleConverter}}"
Value="{Binding AttitudeFrequency, Mode=TwoWay, Delay=2000, Converter={c:UIntToDoubleConverter}}"
Minimum="{Binding AttitudeFrequencyMin, Converter={c:UIntToDoubleConverter}}"
Maximum="{Binding AttitudeFrequencyMax, Converter={c:UIntToDoubleConverter}}"
Focusable="False"
TickPlacement="Both"
IsSnapToTickEnabled="True" />
Expand All @@ -141,7 +135,7 @@
<Label
Grid.Row="0"
Grid.Column="0"
Visibility="{Binding BroadcastHintVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding BroadcastHintVisible, Converter={c:BooleanToVisibilityConverter}}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontStyle="Italic"
Expand Down Expand Up @@ -177,7 +171,7 @@
Width="10" Height="10"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Visibility="{Binding IndicatorVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding IndicatorVisible, Converter={c:BooleanToVisibilityConverter}}"
Margin="25,0,0,0" />
<Label
Grid.Row="1"
Expand All @@ -186,7 +180,7 @@
Foreground="DarkGray"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Visibility="{Binding NotLabelVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding NotLabelVisible, Converter={c:BooleanToVisibilityConverter}}"
Margin="15,0,0,0" />
<Label
Grid.Row="1"
Expand All @@ -195,7 +189,7 @@
Foreground="DarkGray"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Visibility="{Binding ConnectedLabelVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding ConnectedLabelVisible, Converter={c:BooleanToVisibilityConverter}}"
Margin="43,0,0,0" />
<Label
Grid.Row="1"
Expand All @@ -204,12 +198,12 @@
Foreground="OrangeRed"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Visibility="{Binding ErrorLabelVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding ErrorLabelVisible, Converter={c:BooleanToVisibilityConverter}}"
Margin="15,0,0,0" />
<Label
Grid.Row="1"
Grid.Column="0"
Visibility="{Binding UpdateMsgVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding UpdateMsgVisible, Converter={c:BooleanToVisibilityConverter}}"
VerticalAlignment="Center"
HorizontalAlignment="Right"
FontStyle="Italic"
Expand Down Expand Up @@ -246,7 +240,7 @@
RelativeSource="{
RelativeSource FindAncestor,
AncestorType=ui:SplitView}"
Converter="{StaticResource BooleanToIntConverter}"
Converter="{c:BooleanToDoubleConverter}"
ConverterParameter="270,90" />
</i:ChangePropertyAction.Value>
</i:ChangePropertyAction>
Expand Down

0 comments on commit b006879

Please sign in to comment.