Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

已答复 Filter enum values in ObjectDataProvider ?

All Replies

  • Thursday, July 08, 2010 2:59 AM
    Moderator
     
     Answered Has Code

    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 AM
     
     
    Thanks, this is very similar to the solution came up with myself after a good nights sleep ;P Late-night programming doesn't pay off :)