Skip to content

WPF data bindings : Binding to Nested Class

Today I come across interesting data binding situation. I wanted to display a ListBox control binded to all possible values of specific enum. The challenging part was the fact that enum was defined inside static dictionary class. Apparently, you shall use the plus + sign in order to achieve the required result.

1 namespace DataBindings 2 { 3 public static class Terms 4 { 5 public enum MyColor 6 { 7 Red, 8 Green, 9 Blue 10 } ; 11 }12 }

And the xaml is listed bellow. Please notice the plus sign in the line 13:

1 <Window x:Class="WpfDataBindings.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Height="100" Width="150" 5 xmlns:myns="clr-namespace:DataBindings" 6 xmlns:sys="clr-namespace:System;assembly=mscorlib" 7 > 8 <Window.Resources> 9 <ObjectDataProvider MethodName="GetValues" 10 ObjectType="{x:Type sys:Enum}" 11 x:Key="MyColor">12 <ObjectDataProvider.MethodParameters>13 <x:Type TypeName="myns:Terms+MyColor" />14 </ObjectDataProvider.MethodParameters>15 </ObjectDataProvider>16 </Window.Resources>17 <Grid>18 <ListBox 19 ItemsSource="{Binding Source={StaticResource MyColor}}"20 />21 </Grid>22 </Window

Post a Comment

You must be logged in to post a comment.