Skip to content

Commit 2985f1e

Browse files
update 'readme.md'
1 parent af7d4b1 commit 2985f1e

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# Lambda Converters [![NuGet](https://img.shields.io/nuget/v/LambdaConverters.svg)](https://www.nuget.org/packages/LambdaConverters) [![ReSharper-Gallery](https://img.shields.io/badge/resharper--gallery-v1.0.0-lightgrey.svg)](https://resharper-plugins.jetbrains.com/packages/LambdaConverters.Annotations)
1+
# Lambda Converters [![NuGet](https://img.shields.io/nuget/v/LambdaConverters.svg)](https://www.nuget.org/packages/LambdaConverters) [![ReSharper-Gallery](https://img.shields.io/badge/resharper--gallery-v3.0.0-lightgrey.svg)](https://resharper-plugins.jetbrains.com/packages/LambdaConverters.Annotations)
22

3-
The library allows to create `IValueConverter` and `IMultiValueConverter` objects with the most convenient syntax available, ideally, using the lambda expressions.
3+
The library allows to create `IValueConverter`, `IMultiValueConverter`, `DataTemplateSelector`, and `ValidationRule` objects with the most convenient syntax available, ideally, using the lambda expressions.
4+
5+
## Lambda Value Converters
46

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

@@ -26,43 +28,37 @@ You're done! Just reference the converters with the `x:Static` expressions from
2628
<TextBlock Text="{Binding model.Heading, Converter={x:Static c:Converters.ToUpperCase}}" />
2729
```
2830

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

37-
:bulb: *ReSharper users*: use the Extension Manager to install the external annotations for the library.
38-
39-
## Lambda DataTemplateSelectors
39+
## Lambda Data Template Selectors
4040

41-
The library also allows to create `DataTemplateSelector` objects in the same convenient way as Converters. In order to define a Selector simply write a static field (or property) similar to this snippet:
41+
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:
4242

4343
```csharp
4444
internal static class TemplateSelector
4545
{
46-
public static DataTemplateSelector AlternatingText =
47-
LambdaConverters.TemplateSelector.Create<int>(
48-
e => e.Item % 2 == 0
49-
? (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("BlackWhite")
46+
public static DataTemplateSelector AlternatingText =
47+
LambdaConverters.TemplateSelector.Create<int>(
48+
e => e.Item % 2 == 0
49+
? (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("BlackWhite")
5050
: (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("WhiteBlack"));
5151
}
5252
```
53-
Use your Lambda DataTemplateSelectors by referencing it with the `x:Static` markup extention:
53+
Use your Lambda DataTemplateSelectors by referencing it with the `x:Static` markup extention (assuming that `s` is the namespace definition for the `TemplateSelector` class):
5454

5555
```xml
5656
<UserControl.Resources>
5757
<DataTemplate x:Key="BlackWhite">
58-
<TextBlock Text="{Binding}"
59-
Foreground="Black"
60-
Background="White" />
58+
<TextBlock Text="{Binding}" Foreground="Black" Background="White" />
6159
</DataTemplate>
6260
<DataTemplate x:Key="WhiteBlack">
63-
<TextBlock Text="{Binding}"
64-
Foreground="White"
65-
Background="Black" />
61+
<TextBlock Text="{Binding}" Foreground="White" Background="Black" />
6662
</DataTemplate>
6763
</UserControl.Resources>
6864
<DockPanel>
@@ -80,7 +76,7 @@ Tada! All even numbers from `IntNumbers` are displayed with black font and white
8076
- separate class for each selector not needed anymore
8177
- 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).
8278

83-
## Lambda ValidationRules
79+
## Lambda Validation Rules
8480

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

@@ -89,12 +85,12 @@ public static class Rule
8985
{
9086
public static ValidationRule IsNumericString =
9187
LambdaConverters.Validator.Create<string>(
92-
e => e.Value.ToCharArray().All(char.IsDigit)
93-
? ValidationResult.ValidResult
94-
: new ValidationResult(false, "Text has non-digit characters!"));
88+
e => e.Value.All(char.IsDigit)
89+
? ValidationResult.ValidResult
90+
: new ValidationResult(false, "Text has non-digit characters!"));
9591
}
9692
```
97-
And then reference your new rule in vour `View`:
93+
And then reference your new rule in vour `View` (assuming that `r` is the namespace definition for the `Rule` class):
9894
```xml
9995
<TextBox>
10096
<TextBox.Text>
@@ -117,6 +113,8 @@ Now, you made sure that only strings which consists of digits are passed to your
117113
## Installation
118114
Use the NuGet package manager to install the package.
119115

116+
:bulb: *ReSharper users*: use the Extension Manager to install the external annotations for the library.
117+
120118
## Limitations
121119
The library currently supports the WPF only.
122120

0 commit comments

Comments
 (0)