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
- Host: GitHub
- URL: https://github.com/michael-damatov/lambda-converters
- Owner: michael-damatov
- License: apache-2.0
- Created: 2016-01-28T20:30:43.000Z (about 9 years ago)
- Default Branch: develop
- Last Pushed: 2020-01-20T20:52:56.000Z (over 5 years ago)
- Last Synced: 2025-04-05T02:35:20.970Z (25 days ago)
- Topics: converter, data-template, data-template-selector, lambda, lambda-converters, lambda-datatemplateselectors, lambda-expressions, lambda-validationrules, nuget, selector, strongly-typed, validation-rule, wpf
- Language: C#
- Homepage:
- Size: 1.15 MB
- Stars: 142
- Watchers: 8
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Lambda Converters [](https://www.nuget.org/packages/LambdaConverters) [](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).