views:

3685

answers:

2

Hi, I have searched around and it seems very easy to bind enums to combobox, just retrieve Enum values as a list of strings via an ObjectDataProvider from the static Enum.GetValues method, however i can't get it to work. The error is Type ContactExportType was not found.

I have an enum called ContactExportType, it resides on Enums class. This class is part of the CEM.Marketing.Objects namespace.

This is what i have:

<UserControl 
 xmlns:local="clr-namespace:CEM.Marketing.Objects"
 xmlns:sys="clr-namespace:System;assembly=mscorlib">

<Grid>
<Grid.Resources>
        <ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="ContactExportTypes">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:ContactExportType" />
        </ObjectDataProvider.MethodParameters>

    </ObjectDataProvider>
    </Grid.Resources>

</Grid>
 <ComboBox 
        ItemsSource="{Binding {StaticResource ContactExportTypes}}"
...

Thanks, Angela

+1  A: 
<ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type local:Enums}"
                    x:Key="ContactExportTypes">

should be

<ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="ContactExportTypes">

and

<x:Type TypeName="local:ContactExportType" />

should be

<x:Type TypeName="CEM.Marketing.Objects.ContactExportType"/>

the sys:Enum points to the Enum framework class the typename in the parameter points to your fully qualified type-name.

check Bea Stollnitz's blog

    <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odp">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="namespace.class.TShirtSizes"/>
            </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" IsSynchronizedWithCurrentItem="true"/>
Muad'Dib
If TShirtSizes is in different class and namespace, how do you specify it? See my code...Thanks, Angela
Angela
you have to add an xmlns namespace to referance it---- xmlns:myenum="assembly;..."
Muad'Dib
your local xmlns is fine (assuming that is where you enum is at)
Muad'Dib
I changed ObjectType="{x:Type local:Enums}" to ObjectType="{x:Type sys:Enum}", still the same problem..
Angela
change your <x:Type TypeName="local:ContactExportType" /> to "CEM.Marketing.Objects.ContactExportType"
Muad'Dib
Thanks so much for your quick response. now i got an error: TyCEM.Marketing.Objects.ContactExportType' was not found..
Angela
+4  A: 

To access a nested type, you should use the "+" separator :

<ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="ContactExportTypes">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="local:Enums+ContactExportType" />
    </ObjectDataProvider.MethodParameters>

</ObjectDataProvider>

By the way, there is a simpler way to bind to the values of an enum, without using an ObjectDataProvider. It's based on a custom markup extension :

<ComboBox ItemsSource="{local:EnumValues local:Enums+ContactExportType}"/>

Here is the code for the EnumValues markup extension :

[MarkupExtensionReturnType(typeof(object[]))]
public class EnumValuesExtension : MarkupExtension
{
    public EnumValuesExtension()
    {
    }

    public EnumValuesExtension(Type enumType)
    {
        this.EnumType = enumType;
    }

    [ConstructorArgument("enumType")]
    public Type EnumType { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (this.EnumType == null)
            throw new ArgumentException("The enum type is not set");
        return Enum.GetValues(this.EnumType);
    }
}
Thomas Levesque