Daniel's XL Toolbox NG
Bovender.Mvvm.EnumProvider< T > Class Template Reference

Facilitates WPF data binding to enums by providing an enumeration of Choices, read/write access to a string representation (which may be localized in derived classes), and the type-safe enum value itself. More...

Detailed Description

Facilitates WPF data binding to enums by providing an enumeration of Choices, read/write access to a string representation (which may be localized in derived classes), and the type-safe enum value itself.

When providing a public EnumProvider property, you need to wire up the PropertyChanged event:

public EnumProvider<MyType> MyProperty
{
get
{
if (_myPropertyField == null)
{
_myPropertyField = new EnumProvider<MyType>(_myModel.Property);
_myPropertyField.PropertyChanged += (sender, args) =>
{
_myModel.Property = _myPropertyField.AsEnum;
};
}
return _myPropertyField;
}
}

To bind to a ComboBox to an EnumProvider property, use:

<ComboBox
ItemsSource="{Binding MyEnumProviderProperty.Choices}"
ToolTip="{Binding MyEnumProviderProperty.ToolTip}"
SelectedItem="{Binding MyEnumProviderProperty.SelectedItem}"
/>

To make use of per-item enabled states and tool tips, it is helpful to define a generic style in a central resource dictionary:

<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Control.ToolTip" Value="{Binding Path=ToolTip, Mode=OneWay}" />
<Setter Property="IsEnabled" Value="{Binding Path=IsEnabled, Mode=OneWay}" />
</Style>

Since generic type parameters cannot be enums, the workaround "struct, IConvertible" is used here as suggested in http://stackoverflow.com/q/79126/270712

Type Constraints
T :struct 
T :IConvertible 

The documentation for this class was generated from the following file: