Skip to content

Commit 23493ca

Browse files
Merge branch 'feature/release-finalization'
2 parents be0a05a + 2985f1e commit 23493ca

36 files changed

+952
-530
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
- For typos and spelling mistakes feel free to provide a fix and they will be merged as long they comply with the owner's style guide.
66
- For code changes always open an issue first. This will allow to determine whether or not the change should take place. The issue should be used to discuss the change.
77

8-
- If the pull request is accepted the project collaborators preserve the right to fix the code style before merging the changes
8+
- If the pull request is accepted the project collaborators preserve the right to fix the code style before merging the changes.
9+
10+
- Pull requests must target the `develop` branch. Pull requests to the `master` branch will be declined.

README.md

Lines changed: 80 additions & 4 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,19 +28,93 @@ 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.
39+
## Lambda Data Template Selectors
40+
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:
42+
43+
```csharp
44+
internal static class TemplateSelector
45+
{
46+
public static DataTemplateSelector AlternatingText =
47+
LambdaConverters.TemplateSelector.Create<int>(
48+
e => e.Item % 2 == 0
49+
? (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("BlackWhite")
50+
: (DataTemplate) ((FrameworkElement) e.Container)?.FindResource("WhiteBlack"));
51+
}
52+
```
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):
54+
55+
```xml
56+
<UserControl.Resources>
57+
<DataTemplate x:Key="BlackWhite">
58+
<TextBlock Text="{Binding}" Foreground="Black" Background="White" />
59+
</DataTemplate>
60+
<DataTemplate x:Key="WhiteBlack">
61+
<TextBlock Text="{Binding}" Foreground="White" Background="Black" />
62+
</DataTemplate>
63+
</UserControl.Resources>
64+
<DockPanel>
65+
<ListBox ItemsSource="{Binding IntNumbers}"
66+
ItemTemplateSelector="{x:Static s:TemplateSelector.AlternatingText}">
67+
</ListBox>
68+
</DockPanel>
69+
```
70+
71+
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.
72+
73+
### Features
74+
- *strongly-typed* Selectors
75+
- resource declaration not needed, just use the `x:Static` expressions
76+
- separate class for each selector not needed anymore
77+
- 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).
78+
79+
## Lambda Validation Rules
80+
81+
Furthermore, you'll get Lambda ValidationRules on top. By now you know "the drill". First, define a `ValidationRule`object like this:
82+
83+
```csharp
84+
public static class Rule
85+
{
86+
public static ValidationRule IsNumericString =
87+
LambdaConverters.Validator.Create<string>(
88+
e => e.Value.All(char.IsDigit)
89+
? ValidationResult.ValidResult
90+
: new ValidationResult(false, "Text has non-digit characters!"));
91+
}
92+
```
93+
And then reference your new rule in vour `View` (assuming that `r` is the namespace definition for the `Rule` class):
94+
```xml
95+
<TextBox>
96+
<TextBox.Text>
97+
<Binding Path="Text" UpdateSourceTrigger="PropertyChanged">
98+
<Binding.ValidationRules>
99+
<x:Static Member="r:Rule.IsNumericString"/>
100+
</Binding.ValidationRules>
101+
</Binding>
102+
</TextBox.Text>
103+
</TextBox>
104+
```
105+
Now, you made sure that only strings which consists of digits are passed to your `ViewModel`.
106+
107+
### Features
108+
- *strongly-typed* rules
109+
- resource declaration not needed, just use the `x:Static` expressions
110+
- separate class for each rule not needed anymore
111+
- full support for the remaining parameter `culture`
38112

39113
## Installation
40114
Use the NuGet package manager to install the package.
41115

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

Sources/LambdaConverters.Deployment/LambdaConverters.Deployment.csproj

Lines changed: 0 additions & 89 deletions
This file was deleted.

Sources/LambdaConverters.Deployment/LambdaConverters.nuspec

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)