ValueConverters.NET

repository·develop·Indexed 41 days ago

https://github.com/thomasgalliker/valueconverters.net

A library of common IValueConverter implementations for .NET applications, designed to facilitate value conversion between Views and ViewModels in XAML-based frameworks. Available as ValueConverters for standard .NET projects and ValueConverters.MAUI for .NET MAUI projects.

Tokens
1K
Snippets
3
Records
4
Agent score
2%

What's inside ValueConverters.NET

  1. How EnumWrapperConverter works for localized enums

    develop

    The EnumWrapperConverter allows you to display localized strings for enum values. It works by reading [Display] attributes on enum members that point to a resource file.

    Implementation Steps:

    1. Annotate the Enum: Define your enum and use the [Display] attribute on members. Specify the Name (the key in your resource file) and the ResourceType (the class containing your resources).
    2. Setup Resources: Create .resx files (e.g., StringResources.resx and StringResources.de.resx) containing the keys defined in your enum attributes.
    3. Expose in ViewModel: Provide the enum property in your ViewModel.
    4. Bind in View: Use the EnumWrapperConverter in your XAML binding.
    // 1. Define and annotate enum
    [DataContract]
    public enum PartyMode
    {
        [EnumMember]
        [Display(Name = "PartyMode_Off", ResourceType = typeof(PartyModeResources))]
        Off
    }
    
    // 2. Expose in ViewModel
    public PartyMode PartyMode
    {
        get { return this.partyMode; }
        set { this.partyMode = value; this.OnPropertyChanged(() => this.PartyMode); }
    }
    <!-- 3. Bind in View -->
    <Label Content="{Binding PartyMode, Converter={StaticResource EnumWrapperConverter}}" />
  2. Use converters in XAML

    develop

    To use a converter in XAML, follow these two steps:

    1. Define the converter in the Resources section of your View (UserControl, Window, Page, etc.). You can specify properties like Format or MinValueString if the specific converter supports them.
    2. Apply the converter to a binding by referencing it as a StaticResource.
    <!-- 1. Define the converter -->
    <UserControl.Resources> 
        <ResourceDictionary> 
            <converters:DateTimeConverter x:Key="DateTimeConverter" Format="d" MinValueString="-"/> 
        </ResourceDictionary> 
    </UserControl.Resources> 
    
    <!-- 2. Apply the converter -->
    <TextBox Text="{Binding EmployeeDetailViewModel.Birthdate, Converter={StaticResource DateTimeConverter}}"/> 
  3. Install ValueConverters via NuGet

    develop

    You can install ValueConverters using the NuGet Package Manager Console. Choose the package based on your target platform:

    • For standard .NET projects: ValueConverters
    • For .NET MAUI projects: ValueConverters.MAUI
    Install-Package ValueConverters
    Install-Package ValueConverters.MAUI
  4. Configure Converter Culture settings

    develop

    Value converters are culture-aware. While the underlying platform usually provides the culture, you can override this behavior using the PreferredCulture property or by setting a global default.

    Override Behaviors

    Use PreferredCulture to select one of the following:

    • PreferredCulture.ConverterCulture: Uses the culture provided by the underlying platform (Default).
    • ConverterCulture.CurrentCulture: Overrides with CultureInfo.CurrentCulture.
    • ConverterCulture.CurrentUICulture: Overrides with CultureInfo.CurrentUICulture.

    Global Configuration

    You can set the default behavior for all converters in your application using: ValueConvertersConfig.DefaultPreferredCulture.

    Note: In WPF, if you change CultureInfo.CurrentCulture or CultureInfo.CurrentUICulture at runtime, you must manually call OnPropertyChanged to ensure the UI updates, as the platform-provided culture parameter may not update automatically.