Filter enum values in ObjectDataProvider ?
-
Sunday, July 04, 2010 10:20 AM
Hello,
Using a ObjectDataProvider like this, http://blog.davidsandor.com/post/WPF-How-to-bind-to-an-enum-in-XAML.aspx, is there a way to filter the enum values so that only a subset of the enumeration, specifically all but the first and last value, are returned?
Thanks,
Per
All Replies
-
Thursday, July 08, 2010 2:59 AMModerator
Hi Per,
You can generate any object data you want by implementing the methed you call in the ObjectDataProvider. The ObjectDatProvider will take any return value of that method as the datasource.
Here is a simple sample code which did what you want. Hope it helps.
MainWindow.xaml
<Window x:Class="BindingTOEnum.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:BindingTOEnum" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ObjectDataProvider x:Key="MidValues" ObjectType="{x:Type local:EnumDataProvider}" MethodName="GetMidValues"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="HorizontalAlignment"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ObjectDataProvider x:Key="AllValues" ObjectType="{x:Type system:Enum}" MethodName="GetValues"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="HorizontalAlignment"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Grid> <StackPanel> <TextBlock>AllValues:</TextBlock> <ListBox ItemsSource="{Binding Source={StaticResource AllValues}}"/> <TextBlock>MidValues:</TextBlock> <ListBox ItemsSource="{Binding Source={StaticResource MidValues}}"/> </StackPanel> </Grid> </Window>C#
public class EnumDataProvider { public Array GetMidValues(Type enumType) { Array allValues = Enum.GetValues(enumType); int arrayLength = allValues.Length - 2; if (arrayLength < 0) { arrayLength = 0; } Object[] midValues = new Object[arrayLength]; for (int i = 0; i < arrayLength; i++) { midValues[i] = allValues.GetValue(i + 1); } return midValues; } }If you still have any doubts or concerns about this issue, please feel free to let me know.
Best regards,
Min
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked As Answer by Per_Malmberg Thursday, July 08, 2010 8:29 AM
-
Thursday, July 08, 2010 8:29 AMThanks, this is very similar to the solution came up with myself after a good nights sleep ;P Late-night programming doesn't pay off :)

