How EnumWrapperConverter works for localized enums
developThe 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:
- Annotate the Enum: Define your enum and use the
[Display]attribute on members. Specify theName(the key in your resource file) and theResourceType(the class containing your resources). - Setup Resources: Create
.resxfiles (e.g.,StringResources.resxandStringResources.de.resx) containing the keys defined in your enum attributes. - Expose in ViewModel: Provide the enum property in your ViewModel.
- Bind in View: Use the
EnumWrapperConverterin 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}}" />