Creating the Flag Enum Bound ComboBox in WPF using xToolkit
This WPF control allows binding the fields of the flag enumeration to the multi-selection combo box.
Create flag enumeration with fields to be bound to the combo box
[Flags] public enum FlagEnumSample_e { None = 0, [EnumDisplayName("Field 1")] Field1 = 1, [Description("Second Field")] Field2 = 2, Field3 = 4, Field2AndField3 = Field2 | Field3, Field4 = 8 }
Default value assigned to title is enumeration name. To assign the custom title inherit DisplayNameAttribute
[AttributeUsage(AttributeTargets.Field)] public class EnumDisplayNameAttribute : DisplayNameAttribute { public EnumDisplayNameAttribute(string dispName) : base(dispName) { } }
Bind the View Model property in the XAML to the Value dependency property.
public class FlagEnumComboBoxControlVM { public FlagEnumSample_e EnumPrp { get; set; } }
<UserControl x:Class="Wpf.Docs.EnumComboBoxControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ctrls="clr-namespace:Xarial.XToolkit.Wpf.Controls;assembly=Xarial.XToolkit.Wpf"> <Grid> <ctrls:FlagEnumComboBox Value="{Binding Path=EnumPrp}"/> </Grid> </UserControl>
Control supports joined values as well as none (0) value. These items will be assigned with blue and gray colors correspondingly.