All Projects → michael-damatov → Lambda Converters

michael-damatov / Lambda Converters

Licence: apache-2.0
Strongly-typed lambda expressions as value converters, data template selectors, and validation rules

Projects that are alternatives of or similar to Lambda Converters

BilibiliLiveRecordDownLoader
Bilibili 直播录制与回放下载
Stars: ✭ 422 (+326.26%)
Mutual labels:  nuget, wpf
ManagedShell
A library for creating Windows shell replacements using .NET.
Stars: ✭ 134 (+35.35%)
Mutual labels:  nuget, wpf
Elysium-Extra
Elysium Extra is a library that implements Metro style for Windows Presentation Foundation (WPF) applications. This Project is a very large add-on project built on top of the Elysium SDK.
Stars: ✭ 65 (-34.34%)
Mutual labels:  nuget, wpf
SciColorMaps
Custom .NET color maps (user-defined or imported from matplotlib) for scientific visualization
Stars: ✭ 26 (-73.74%)
Mutual labels:  nuget, wpf
Wpfdesigner
The WPF Designer from SharpDevelop
Stars: ✭ 479 (+383.84%)
Mutual labels:  nuget, wpf
Aura.ui
A Library with a lot of Controls for AvaloniaUI
Stars: ✭ 114 (+15.15%)
Mutual labels:  nuget, wpf
DevOpsExamples
A repo to show you how to use a private NuGet feed, such as Telerik, to restore packages in Azure DevOps, GitHub Actions, GitLab CI and AppCenter.
Stars: ✭ 16 (-83.84%)
Mutual labels:  nuget, wpf
Caliburn.metro
A library that combines MahApps.Metro with Caliburn.Micro for Metro UI styled WPF applications.
Stars: ✭ 112 (+13.13%)
Mutual labels:  nuget, wpf
Mvvmcross
The .NET MVVM framework for cross-platform solutions, including Xamarin.iOS, Xamarin.Android, Windows and Mac.
Stars: ✭ 3,594 (+3530.3%)
Mutual labels:  nuget, wpf
H.NotifyIcon.WPF
NotifyIcon for .Net Core 3.1 and .Net 5 WPF.
Stars: ✭ 44 (-55.56%)
Mutual labels:  nuget, wpf
ColorPicker
Customizable Color Picker control for WPF
Stars: ✭ 57 (-42.42%)
Mutual labels:  nuget, wpf
Avalonedit
The WPF-based text editor component used in SharpDevelop
Stars: ✭ 1,127 (+1038.38%)
Mutual labels:  nuget, wpf
Toastnotifications
Toast notifications for WPF allows you to create and display rich notifications in WPF applications. It's highly configurable with set of built-in options like positions, behaviours, themes and many others. It's extendable, it gives you possibility to create custom and interactive notifications in simply manner.
Stars: ✭ 507 (+412.12%)
Mutual labels:  nuget, wpf
Blurrycontrols
Small design library for blured controls in XAML and WPF for C#
Stars: ✭ 77 (-22.22%)
Mutual labels:  nuget, wpf
Epoch
A simple but powerful Epoch converter
Stars: ✭ 94 (-5.05%)
Mutual labels:  converter
Kramdown Asciidoc
A kramdown extension for converting Markdown documents to AsciiDoc.
Stars: ✭ 97 (-2.02%)
Mutual labels:  converter
Reactivemvvm
Cross-platform ReactiveUI sample app built for a talk at MSK .NET conf.
Stars: ✭ 94 (-5.05%)
Mutual labels:  wpf
Aws Cli Cheatsheet
☁️ AWS CLI + JQ = Make life easier
Stars: ✭ 94 (-5.05%)
Mutual labels:  lambda
Binance.api.csharp.client
C#.NET client for Binance Exchange API.
Stars: ✭ 98 (-1.01%)
Mutual labels:  nuget
Node Athena
a nodejs simple aws athena client
Stars: ✭ 97 (-2.02%)
Mutual labels:  lambda

Lambda Converters NuGet ReSharper Extension

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):

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

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

    public static readonly IValueConverter ToUpperCase =
        ValueConverter.Create<string, string>(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):

<Button Visibility="{Binding model.IsAvailable, Converter={x:Static c:Converters.VisibleIfTrue}}" />

<TextBlock Text="{Binding model.Heading, Converter={x:Static c:Converters.ToUpperCase}}" />

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 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:

internal static class TemplateSelector
{
    public static DataTemplateSelector AlternatingText =
        LambdaConverters.TemplateSelector.Create<int>(
            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):

<UserControl.Resources>
    <DataTemplate x:Key="BlackWhite">
        <TextBlock Text="{Binding}" Foreground="Black" Background="White" />
    </DataTemplate>
    <DataTemplate x:Key="WhiteBlack">
        <TextBlock Text="{Binding}" Foreground="White" Background="Black" />
    </DataTemplate>
</UserControl.Resources>
<DockPanel>
    <ListBox ItemsSource="{Binding IntNumbers}"
             ItemTemplateSelector="{x:Static s:TemplateSelector.AlternatingText}">
    </ListBox>
</DockPanel>

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 ValidationRuleobject like this:

public static class Rule
{
    public static ValidationRule IsNumericString =
        LambdaConverters.Validator.Create<string>(
            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):

<TextBox>
    <TextBox.Text>
        <Binding Path="Text" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <x:Static Member="r:Rule.IsNumericString"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

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.

💡 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].