Skip to content

Commit

Permalink
Merge pull request #6 from ChrisPulman/AddConverters
Browse files Browse the repository at this point in the history
Add Converters
  • Loading branch information
ChrisPulman authored Jan 20, 2024
2 parents a63f318 + 46b2718 commit 990bbc9
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Converters available are:
- VisibilityFromNumberConverter
- VisibilityFromNumberEqualsConverter
- ZeroToVisibilityConverter
- BrushToColorConverter
- FallbackBrushConverter

## Usage

Expand Down
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0.1",
"version": "1.0.2",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
2 changes: 1 addition & 1 deletion src/XamlConverters/ArithmeticConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ object IValueConverter.Convert(object value, Type targetType, object parameter,
/// <param name="culture">The parameter is not used.</param>
/// <returns>The parameter is not used.</returns>
/// <exception cref="Exception">An Exception.</exception>
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new Exception("The method or operation is not implemented.");
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ public virtual object Convert(object value, Type targetType, object parameter, C
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/Boolean/NullToBoolConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ public class NullToBoolConverter : IValueConverter
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/Boolean/ValueNotNullToBoolConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
/// <exception cref="NotImplementedException">Not Implemented Exception.</exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
44 changes: 44 additions & 0 deletions src/XamlConverters/BrushToColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace CP.Xaml.Converters;

/// <summary>
/// BrushToColorConverter.
/// </summary>
/// <seealso cref="System.Windows.Data.IValueConverter" />
public class BrushToColorConverter : IValueConverter
{
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) => value switch
{
SolidColorBrush brush => brush.Color,
Color color => color,
_ => Colors.Red
};

/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="targetType">The type to convert to.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing;
}
44 changes: 44 additions & 0 deletions src/XamlConverters/FallbackBrushConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace CP.Xaml.Converters;

/// <summary>
/// FallbackBrushConverter.
/// </summary>
/// <seealso cref="System.Windows.Data.IValueConverter" />
public class FallbackBrushConverter : IValueConverter
{
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) => value switch
{
SolidColorBrush brush => brush,
Color color => new SolidColorBrush(color),
_ => new SolidColorBrush(Colors.Red)
};

/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="targetType">The type to convert to.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/IntToThicknessConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/IsGreaterThanOrEqualToConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;

/// <summary>
/// Converts a binding target value to the source binding values.
Expand Down
2 changes: 1 addition & 1 deletion src/XamlConverters/LineStrokeLevelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The parameter.</param>
/// <param name="culture">The culture.</param>
/// <returns>A Value.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/MathConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
/// <exception cref="NotImplementedException">Not Implemented Exception.</exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;

/// <summary>
/// Converts a binding target value to the source binding values.
Expand Down
2 changes: 1 addition & 1 deletion src/XamlConverters/MultiConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ public class MultiConverter : List<IValueConverter>, IValueConverter
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/ToLowerConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public class ToLowerConverter : IValueConverter
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/ToUpperConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public class ToUpperConverter : IValueConverter
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
/// <exception cref="NotImplementedException">Not Implemented Exception.</exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/ValueGtXConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The parameter is not used.</param>
/// <param name="culture">The parameter is not used.</param>
/// <returns>The parameter is not used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
2 changes: 1 addition & 1 deletion src/XamlConverters/ValueToBrushConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The parameter.</param>
/// <param name="culture">The culture.</param>
/// <returns>A Value.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
4 changes: 2 additions & 2 deletions src/XamlConverters/Visibility/NullToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <exception cref="NotImplementedException">
/// The method or operation is not implemented.
/// </exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
/// <exception cref="Exception">The method or operation is not implemented.</exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new Exception("The method or operation is not implemented.");
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}
4 changes: 2 additions & 2 deletions src/XamlConverters/Visibility/ZeroToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
/// <exception cref="NotImplementedException">Not Implemented Exception.</exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}

0 comments on commit 990bbc9

Please sign in to comment.