On a recent demonstration about Silverlight data binding I wanted to bind some RadioButtons in the UI to each possible value of an Enum type; the selected RadioButton would bring the actual value of the enumeration. The following code snippet shows the enumeration.
1 2 3 4 5 6 | public enum Sizes { Small = 3, Medium = 4, Large = 5 } |
1 2 3 4 5 6 | <!-- XAML Snippet --> < RadioButton GroupName = "SizeGroup" IsChecked="{Binding Sizes, Mode = TwoWay , Converter={StaticResource Matcher}, ConverterParameter = Small }"> ... </ RadioButton > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!-- Custom IValueConverter --> public class MatchingEnumConverter : IValueConverter { public object Convert( object value, Type targetType, object desiredValue, CultureInfo culture) { return Enum.Parse( value.GetType(), desiredValue.ToString(), true ).Equals(value); } public object ConvertBack( object value, Type targetType, object desiredValue, CultureInfo culture) { return ( bool )value ? desiredValue : null ; } } |
1 comment:
thanks!
Post a Comment