Skip to content

Commit b1d9629

Browse files
committed
Updated the README.md with descriptions of the Lambda DataTemplateSelector and the Lambda ValidationRule.
Signed-off-by: Dima Enns <[email protected]>
1 parent aca66f0 commit b1d9629

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,84 @@ You're done! Just reference the converters with the `x:Static` expressions from
3636

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

39+
## Lambda DataTemplateSelectors
40+
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:
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:
54+
55+
```xml
56+
<UserControl.Resources>
57+
<DataTemplate x:Key="BlackWhite">
58+
<TextBlock Text="{Binding}"
59+
Foreground="Black"
60+
Background="White" />
61+
</DataTemplate>
62+
<DataTemplate x:Key="WhiteBlack">
63+
<TextBlock Text="{Binding}"
64+
Foreground="White"
65+
Background="Black" />
66+
</DataTemplate>
67+
</UserControl.Resources>
68+
<DockPanel>
69+
<ListBox ItemsSource="{Binding IntNumbers}"
70+
ItemTemplateSelector="{x:Static s:TemplateSelector.AlternatingText}">
71+
</ListBox>
72+
</DockPanel>
73+
```
74+
75+
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.
76+
77+
### Features
78+
- *strongly-typed* Selectors
79+
- resource declaration not needed, just use the `x:Static` expressions
80+
- separate class for each selector not needed anymore
81+
- 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).
82+
83+
## Lambda ValidationRules
84+
85+
Furthermore, you'll get Lambda ValidationRules on top. By now you know "the drill". First, define a `ValidationRule`object like this:
86+
87+
```csharp
88+
public static class Rule
89+
{
90+
public static ValidationRule IsNumericString =
91+
LambdaConverters.Validator.Create<string>(
92+
e => e.Value.ToCharArray().All(char.IsDigit)
93+
? ValidationResult.ValidResult
94+
: new ValidationResult(false, "Text has non-digit characters!"));
95+
}
96+
```
97+
And then reference your new rule in vour `View`:
98+
```xml
99+
<TextBox>
100+
<TextBox.Text>
101+
<Binding Path="Text" UpdateSourceTrigger="PropertyChanged">
102+
<Binding.ValidationRules>
103+
<x:Static Member="r:Rule.IsNumericString"/>
104+
</Binding.ValidationRules>
105+
</Binding>
106+
</TextBox.Text>
107+
</TextBox>
108+
```
109+
Now, you made sure that only strings which consists of digits are passed to your `ViewModel`.
110+
111+
### Features
112+
- *strongly-typed* rules
113+
- resource declaration not needed, just use the `x:Static` expressions
114+
- separate class for each rule not needed anymore
115+
- full support for the remaining parameter `culture`
116+
39117
## Installation
40118
Use the NuGet package manager to install the package.
41119

0 commit comments

Comments
 (0)