-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added bool to double converter (#310)
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/DIPS.Xamarin.UI/Converters/ValueConverters/BoolToDoubleConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using DIPS.Xamarin.UI.Internal.Utilities; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Xaml; | ||
|
||
namespace DIPS.Xamarin.UI.Converters.ValueConverters | ||
{ | ||
/// <summary> | ||
/// Converts a boolean input value to it's respective <see cref="TrueDouble"/> or <see cref="FalseDouble"/> depending on the <see cref="Inverted"/> value | ||
/// </summary> | ||
public class BoolToDoubleConverter : IValueConverter, IMarkupExtension | ||
{ | ||
private IServiceProvider m_serviceProvider; | ||
|
||
/// <summary> | ||
/// The value that will return if the boolean input is true | ||
/// <remarks>Will be the return value if <see cref="Inverted" /> is set to true</remarks> | ||
/// </summary> | ||
public double TrueDouble { get; set; } | ||
|
||
/// <summary> | ||
/// The value that will return if the boolean input is false | ||
/// <remarks>Will be the return value if <see cref="Inverted" /> is set to false</remarks> | ||
/// </summary> | ||
public double FalseDouble { get; set; } | ||
|
||
/// <summary> | ||
/// A boolean value to set if the output value should be inverted | ||
/// </summary> | ||
public bool Inverted { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value is not bool inputValue) | ||
{ | ||
throw new XamlParseException($"Input value has to be of type {nameof(Boolean)}").WithXmlLineInfo( | ||
m_serviceProvider); | ||
} | ||
|
||
return Inverted ? inputValue ? FalseDouble : TrueDouble : inputValue ? TrueDouble : FalseDouble; | ||
} | ||
|
||
/// <inheritdoc /> | ||
[ExcludeFromCodeCoverage] | ||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
[ExcludeFromCodeCoverage] | ||
public object ProvideValue(IServiceProvider serviceProvider) | ||
{ | ||
m_serviceProvider = serviceProvider; | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...Samples/DIPS.Xamarin.UI.Samples/Converters/ValueConverters/BoolToDoubleConverterPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:valueConverters="http://dips.xamarin.ui.com" | ||
xmlns:valueConverters1="clr-namespace:DIPS.Xamarin.UI.Samples.Converters.ValueConverters;assembly=DIPS.Xamarin.UI.Samples" | ||
x:Class="DIPS.Xamarin.UI.Samples.Converters.ValueConverters.BoolToDoubleConverterPage"> | ||
<ContentPage.BindingContext> | ||
<valueConverters1:BoolToDoubleConverterViewModel /> | ||
</ContentPage.BindingContext> | ||
<ContentPage.Content> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition /> | ||
</Grid.RowDefinitions> | ||
|
||
<StackLayout Grid.Row="0" | ||
Orientation="Horizontal"> | ||
<CheckBox IsChecked="{Binding SomeLogicalProperty}" /> | ||
<Label Text="Set me" VerticalOptions="Center" /> | ||
</StackLayout> | ||
|
||
<StackLayout Grid.Row="1"> | ||
<Label | ||
Opacity="{Binding SomeLogicalProperty, Converter={valueConverters:BoolToDoubleConverter TrueDouble=0.5, FalseDouble=1}}" | ||
Text="My opacity changes" /> | ||
<Label | ||
Opacity="{Binding SomeLogicalProperty, Converter={valueConverters:BoolToDoubleConverter TrueDouble=0.5, FalseDouble=1, Inverted=True}}" | ||
Text="My opacity is inverted" /> | ||
</StackLayout> | ||
</Grid> | ||
</ContentPage.Content> | ||
</ContentPage> |
34 changes: 34 additions & 0 deletions
34
...ples/DIPS.Xamarin.UI.Samples/Converters/ValueConverters/BoolToDoubleConverterPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DIPS.Xamarin.UI.Extensions; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Xaml; | ||
|
||
namespace DIPS.Xamarin.UI.Samples.Converters.ValueConverters | ||
{ | ||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
public partial class BoolToDoubleConverterPage : ContentPage | ||
{ | ||
public BoolToDoubleConverterPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
|
||
public class BoolToDoubleConverterViewModel : INotifyPropertyChanged | ||
{ | ||
private bool m_someLogicalProperty; | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
public bool SomeLogicalProperty | ||
{ | ||
get => m_someLogicalProperty; | ||
set => this.Set(ref m_someLogicalProperty, value, PropertyChanged); | ||
} | ||
|
||
} | ||
} |