{"id":19992894,"url":"https://github.com/michael-damatov/lambda-converters","last_synced_at":"2025-04-05T20:17:21.548Z","repository":{"id":53861671,"uuid":"50611540","full_name":"michael-damatov/lambda-converters","owner":"michael-damatov","description":"Strongly-typed lambda expressions as value converters, data template selectors, and validation rules","archived":false,"fork":false,"pushed_at":"2020-01-20T20:52:56.000Z","size":1210,"stargazers_count":142,"open_issues_count":1,"forks_count":15,"subscribers_count":8,"default_branch":"develop","last_synced_at":"2025-04-05T02:35:20.970Z","etag":null,"topics":["converter","data-template","data-template-selector","lambda","lambda-converters","lambda-datatemplateselectors","lambda-expressions","lambda-validationrules","nuget","selector","strongly-typed","validation-rule","wpf"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/michael-damatov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-28T20:30:43.000Z","updated_at":"2025-04-01T09:35:05.000Z","dependencies_parsed_at":"2022-08-25T03:20:26.595Z","dependency_job_id":null,"html_url":"https://github.com/michael-damatov/lambda-converters","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-damatov%2Flambda-converters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-damatov%2Flambda-converters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-damatov%2Flambda-converters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-damatov%2Flambda-converters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michael-damatov","download_url":"https://codeload.github.com/michael-damatov/lambda-converters/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393568,"owners_count":20931813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converter","data-template","data-template-selector","lambda","lambda-converters","lambda-datatemplateselectors","lambda-expressions","lambda-validationrules","nuget","selector","strongly-typed","validation-rule","wpf"],"created_at":"2024-11-13T04:52:22.096Z","updated_at":"2025-04-05T20:17:21.524Z","avatar_url":"https://github.com/michael-damatov.png","language":"C#","funding_links":[],"categories":["C\\#"],"sub_categories":[],"readme":"# Lambda Converters [![NuGet](https://img.shields.io/nuget/v/LambdaConverters.svg)](https://www.nuget.org/packages/LambdaConverters) [![ReSharper Extension](https://img.shields.io/resharper/v/LambdaConverters.Annotations.svg?label=ReSharper%20Extension)](https://plugins.jetbrains.com/plugin/11659-lambda-converters-annotations)\n\nThe library allows to create `IValueConverter`, `IMultiValueConverter`, `DataTemplateSelector`, and `ValidationRule` objects with the most convenient syntax available, ideally, using the lambda expressions.\n\n## Lambda Value Converters\n\nFirst create a (static) class and define your converters as static fields (or properties):\n\n```csharp\ninternal static class Converters\n{\n    public static readonly IValueConverter VisibleIfTrue =\n        ValueConverter.Create\u003cbool, Visibility\u003e(e =\u003e e.Value ? Visibility.Visible : Visibility.Collapsed);\n\n    public static readonly IValueConverter VisibleIfNotNull =\n        ValueConverter.Create\u003cobject, Visibility\u003e(e =\u003e e.Value != null ? Visibility.Visible : Visibility.Collapsed);\n\n    public static readonly IValueConverter ToUpperCase =\n        ValueConverter.Create\u003cstring, string\u003e(e =\u003e e.Value.ToUpper());\n}\n```\n\nYou'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):\n\n```xml\n\u003cButton Visibility=\"{Binding model.IsAvailable, Converter={x:Static c:Converters.VisibleIfTrue}}\" /\u003e\n\n\u003cTextBlock Text=\"{Binding model.Heading, Converter={x:Static c:Converters.ToUpperCase}}\" /\u003e\n```\n\n### Features\n- *strongly-typed* converters\n- resource declaration not needed, just use the `x:Static` expressions\n- separate class for each converter not needed anymore\n- no redundant declarations: if you do not need the `ConvertBack` method, don't define it; otherwise, just put the second lambda expression\n- full support for the remaining parameters of the `Convert` and `ConvertBack` methods: the `culture` and the `parameter` (also strongly-typed) are accessible as well\n- if the conversion fails due to unexpected value types the optional [error strategy](Sources/LambdaConverters.Wpf/ConverterErrorStrategy.cs) can be specified\n\n## Lambda Data Template Selectors\n\nThe 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:\n\n```csharp\ninternal static class TemplateSelector\n{\n    public static DataTemplateSelector AlternatingText =\n        LambdaConverters.TemplateSelector.Create\u003cint\u003e(\n            e =\u003e e.Item % 2 == 0\n                ? (DataTemplate) ((FrameworkElement) e.Container)?.FindResource(\"BlackWhite\")\n                : (DataTemplate) ((FrameworkElement) e.Container)?.FindResource(\"WhiteBlack\"));\n}\n```\nUse your Lambda DataTemplateSelectors by referencing it with the `x:Static` markup extention (assuming that `s` is the namespace definition for the `TemplateSelector` class):\n\n```xml\n\u003cUserControl.Resources\u003e\n    \u003cDataTemplate x:Key=\"BlackWhite\"\u003e\n        \u003cTextBlock Text=\"{Binding}\" Foreground=\"Black\" Background=\"White\" /\u003e\n    \u003c/DataTemplate\u003e\n    \u003cDataTemplate x:Key=\"WhiteBlack\"\u003e\n        \u003cTextBlock Text=\"{Binding}\" Foreground=\"White\" Background=\"Black\" /\u003e\n    \u003c/DataTemplate\u003e\n\u003c/UserControl.Resources\u003e\n\u003cDockPanel\u003e\n    \u003cListBox ItemsSource=\"{Binding IntNumbers}\"\n             ItemTemplateSelector=\"{x:Static s:TemplateSelector.AlternatingText}\"\u003e\n    \u003c/ListBox\u003e\n\u003c/DockPanel\u003e\n```\n\nTada! All even numbers from `IntNumbers` are displayed with black font and white background and the odd numbers get the inverse font and background colors.\n\n### Features\n- *strongly-typed* Selectors\n- resource declaration not needed, just use the `x:Static` expressions\n- separate class for each selector not needed anymore\n- 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).\n\n## Lambda Validation Rules\n\nFurthermore, you'll get Lambda ValidationRules on top. By now you know \"the drill\". First, define a `ValidationRule`object like this:\n\n```csharp\npublic static class Rule\n{\n    public static ValidationRule IsNumericString =\n        LambdaConverters.Validator.Create\u003cstring\u003e(\n            e =\u003e e.Value.All(char.IsDigit)\n                ? ValidationResult.ValidResult\n                : new ValidationResult(false, \"Text has non-digit characters!\"));\n}\n```\nAnd then reference your new rule in vour `View` (assuming that `r` is the namespace definition for the `Rule` class):\n```xml\n\u003cTextBox\u003e\n    \u003cTextBox.Text\u003e\n        \u003cBinding Path=\"Text\" UpdateSourceTrigger=\"PropertyChanged\"\u003e\n            \u003cBinding.ValidationRules\u003e\n                \u003cx:Static Member=\"r:Rule.IsNumericString\"/\u003e\n            \u003c/Binding.ValidationRules\u003e\n        \u003c/Binding\u003e\n    \u003c/TextBox.Text\u003e\n\u003c/TextBox\u003e\n```\nNow, you made sure that only strings which consists of digits are passed to your `ViewModel`.\n\n### Features\n- *strongly-typed* rules\n- resource declaration not needed, just use the `x:Static` expressions\n- separate class for each rule not needed anymore\n- full support for the remaining parameter `culture`\n\n## Installation\nUse the NuGet package manager to install the package.\n\n:bulb: *ReSharper users*: use the Extension Manager to install the external annotations for the library.\n\n## Limitations\nThe library currently supports the WPF only.\n\n## Bugs? Questions? Suggestions?\nPlease feel free to [report them](https://github.com/michael-damatov/lambda-converters/issues).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichael-damatov%2Flambda-converters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichael-damatov%2Flambda-converters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichael-damatov%2Flambda-converters/lists"}