An open API service indexing awesome lists of open source software.

https://github.com/michael-damatov/lambda-converters

Strongly-typed lambda expressions as value converters, data template selectors, and validation rules
https://github.com/michael-damatov/lambda-converters

converter data-template data-template-selector lambda lambda-converters lambda-datatemplateselectors lambda-expressions lambda-validationrules nuget selector strongly-typed validation-rule wpf

Last synced: 24 days ago
JSON representation

Strongly-typed lambda expressions as value converters, data template selectors, and validation rules

Awesome Lists containing this project

README

        

# Lambda Converters [![NuGet](https://img.shields.io/nuget/v/LambdaConverters.svg)](https://www.nuget.org/packages/LambdaConverters) [![ReSharper Extension](https://img.shields.io/resharper/v/LambdaConverters.Annotations.svg?label=ReSharper%20Extension)](https://plugins.jetbrains.com/plugin/11659-lambda-converters-annotations)

The library allows to create `IValueConverter`, `IMultiValueConverter`, `DataTemplateSelector`, and `ValidationRule` objects with the most convenient syntax available, ideally, using the lambda expressions.

## Lambda Value Converters

First create a (static) class and define your converters as static fields (or properties):

```csharp
internal static class Converters
{
public static readonly IValueConverter VisibleIfTrue =
ValueConverter.Create(e => e.Value ? Visibility.Visible : Visibility.Collapsed);

public static readonly IValueConverter VisibleIfNotNull =
ValueConverter.Create(e => e.Value != null ? Visibility.Visible : Visibility.Collapsed);

public static readonly IValueConverter ToUpperCase =
ValueConverter.Create(e => e.Value.ToUpper());
}
```

You're done! Just reference the converters with the `x:Static` expressions from your XAML files (assuming that `c` is the namespace definition for the `Converters` class):

```xml

```

### Features
- *strongly-typed* converters
- resource declaration not needed, just use the `x:Static` expressions
- separate class for each converter not needed anymore
- no redundant declarations: if you do not need the `ConvertBack` method, don't define it; otherwise, just put the second lambda expression
- full support for the remaining parameters of the `Convert` and `ConvertBack` methods: the `culture` and the `parameter` (also strongly-typed) are accessible as well
- if the conversion fails due to unexpected value types the optional [error strategy](Sources/LambdaConverters.Wpf/ConverterErrorStrategy.cs) can be specified

## Lambda Data Template Selectors

The library also allows to create `DataTemplateSelector` objects in the same convenient way as value converters. In order to define a selector simply write a static field (or property) similar to this snippet:

```csharp
internal static class TemplateSelector
{
public static DataTemplateSelector AlternatingText =
LambdaConverters.TemplateSelector.Create(
e => e.Item % 2 == 0
? (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("BlackWhite")
: (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("WhiteBlack"));
}
```
Use your Lambda DataTemplateSelectors by referencing it with the `x:Static` markup extention (assuming that `s` is the namespace definition for the `TemplateSelector` class):

```xml







```

Tada! All even numbers from `IntNumbers` are displayed with black font and white background and the odd numbers get the inverse font and background colors.

### Features
- *strongly-typed* Selectors
- resource declaration not needed, just use the `x:Static` expressions
- separate class for each selector not needed anymore
- full support for the remaining parameter `container`. For example, if you need to grab a `DataTemplate` from where the selector is use (see the example above).

## Lambda Validation Rules

Furthermore, you'll get Lambda ValidationRules on top. By now you know "the drill". First, define a `ValidationRule`object like this:

```csharp
public static class Rule
{
public static ValidationRule IsNumericString =
LambdaConverters.Validator.Create(
e => e.Value.All(char.IsDigit)
? ValidationResult.ValidResult
: new ValidationResult(false, "Text has non-digit characters!"));
}
```
And then reference your new rule in vour `View` (assuming that `r` is the namespace definition for the `Rule` class):
```xml







```
Now, you made sure that only strings which consists of digits are passed to your `ViewModel`.

### Features
- *strongly-typed* rules
- resource declaration not needed, just use the `x:Static` expressions
- separate class for each rule not needed anymore
- full support for the remaining parameter `culture`

## Installation
Use the NuGet package manager to install the package.

:bulb: *ReSharper users*: use the Extension Manager to install the external annotations for the library.

## Limitations
The library currently supports the WPF only.

## Bugs? Questions? Suggestions?
Please feel free to [report them](https://github.com/michael-damatov/lambda-converters/issues).