locked
Bind enum to combobox RRS feed

  • Question

  • Hello

    I am trying to bind enum to combobox. I was looking on several websites, anyway, can not manage it to working.

    I am having this error:

    {"Type reference cannot find type named '{clr-namespace:exTibia;assembly=exTibia}Consts.Directions'."}

    Below my xaml and class.

    <Window x:Class="exTibia.Settings"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            Title="Settings"
            Height="350"
            Width="550"
            WindowStyle="ToolWindow"
            ResizeMode="NoResize"
            xmlns:local="clr-namespace:exTibia"
            xmlns:System="clr-namespace:System;assembly=mscorlib"
            Closing="Window_Closing"
            WindowStartupLocation="CenterOwner">
        <Window.Resources>
            <ObjectDataProvider  x:Key="ConstsDirections" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:Consts.Directions"></x:Type>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>

    namespace exTibia
    {
        public class Consts
        {
            public enum Directions
            {
                North = 0,
                NorthEast = 5,
                NorthWest = 8,
                South = 2,
                SouthEast = 6,
                SouthWest = 7,
                East = 1,     
                West = 3
            }
        }
    }

    Could anyone help me please?

    /panqnik

    • Moved by Jason Dot Wang Wednesday, October 17, 2012 5:52 AM This thread is not about C# (From:Visual C# General)
    Sunday, October 14, 2012 8:36 PM

Answers

  • Hello panqnik, firstly I don't know the benefits to nest the enum inside a class, because outside all works fine without any problem. Please read first this post:

    http://social.msdn.microsoft.com/Forums/en-NZ/vswpfdesigner/thread/ea668b28-636b-4f99-b1e7-49dc4d98b263

    Anyway, if you continue to get it working, here is a workarround:

    First you need to create a new XamlExtension:

    public class Type2Extension : TypeExtension
    {
        public Type2Extension()
        {
        }
    
        public Type2Extension(string typeName)
        {
            base.TypeName = typeName;
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IXamlTypeResolver typeResolver = (IXamlTypeResolver)serviceProvider.GetService(typeof(IXamlTypeResolver));
            int sepindex = TypeName.IndexOf('+');
            if (sepindex < 0)
                return typeResolver.Resolve(TypeName);
            else
            {
                Type outerType = typeResolver.Resolve(TypeName.Substring(0, sepindex));
                return outerType.Assembly.GetType(outerType.FullName + "+" + TypeName.Substring(sepindex + 1));
            }
        }
    } 
    

    Then, you can use it in your XAML code:

    <Window x:Class="exTibia.Settings"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            Title="Settings"
            Height="350"
            Width="550"
            WindowStyle="ToolWindow"
            ResizeMode="NoResize"
            xmlns:local="clr-namespace:exTibia"
            xmlns:System="clr-namespace:System;assembly=mscorlib"
            Closing="Window_Closing"
            WindowStartupLocation="CenterOwner">
        <Window.Resources>
            <ObjectDataProvider  x:Key="ConstsDirections" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <local:Type2 TypeName="local:Consts+Directions"></x:Type>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>

    Regards,

    Miguel.

    • Marked as answer by panqnik Friday, October 26, 2012 12:44 PM
    Sunday, October 14, 2012 10:15 PM
  • Hi,

    do you need to embed the enum inside the class? You simply could do:

    namespace exTibia
    {
            public enum Directions
            {
                North = 0,
                NorthEast = 5,
                NorthWest = 8,
                South = 2,
                SouthEast = 6,
                SouthWest = 7,
                East = 1,     
                West = 3
            }
    }

    and let the MethodParameters TypeName point to local:Directions

    Regards,

      Thorsten

    • Marked as answer by panqnik Sunday, October 14, 2012 9:14 PM
    • Unmarked as answer by panqnik Sunday, October 14, 2012 9:14 PM
    • Marked as answer by panqnik Friday, October 26, 2012 12:44 PM
    Sunday, October 14, 2012 8:59 PM

All replies

  • Hi,

    do you need to embed the enum inside the class? You simply could do:

    namespace exTibia
    {
            public enum Directions
            {
                North = 0,
                NorthEast = 5,
                NorthWest = 8,
                South = 2,
                SouthEast = 6,
                SouthWest = 7,
                East = 1,     
                West = 3
            }
    }

    and let the MethodParameters TypeName point to local:Directions

    Regards,

      Thorsten

    • Marked as answer by panqnik Sunday, October 14, 2012 9:14 PM
    • Unmarked as answer by panqnik Sunday, October 14, 2012 9:14 PM
    • Marked as answer by panqnik Friday, October 26, 2012 12:44 PM
    Sunday, October 14, 2012 8:59 PM
  • Hello, there is a way to get nested types working in WPF, but I recommend you not to use nested types for binding purposes.

    http://neilmosafi.blogspot.com.es/2007/08/dec-06-wpfxaml-xtype-and-nested-classes.html

    Regards,

    Miguel.

    Sunday, October 14, 2012 9:01 PM
  • I have followed link up, and tried:

    <x:Type TypeName="local:Consts+Directions"></x:Type>

    Anwyay, it doesn't work. Any hints?

    /panqnik

    Sunday, October 14, 2012 9:17 PM
  • ... if you need it inside the class  see:

    http://stackoverflow.com/questions/878356/wpf-combobox-binding-to-enum-what-did-i-do-wrong

    Regards,

      Thorsten

    Sunday, October 14, 2012 9:19 PM
  • I saw this website, it also doesn't work.
    Sunday, October 14, 2012 9:20 PM
  • Hello panqnik, firstly I don't know the benefits to nest the enum inside a class, because outside all works fine without any problem. Please read first this post:

    http://social.msdn.microsoft.com/Forums/en-NZ/vswpfdesigner/thread/ea668b28-636b-4f99-b1e7-49dc4d98b263

    Anyway, if you continue to get it working, here is a workarround:

    First you need to create a new XamlExtension:

    public class Type2Extension : TypeExtension
    {
        public Type2Extension()
        {
        }
    
        public Type2Extension(string typeName)
        {
            base.TypeName = typeName;
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IXamlTypeResolver typeResolver = (IXamlTypeResolver)serviceProvider.GetService(typeof(IXamlTypeResolver));
            int sepindex = TypeName.IndexOf('+');
            if (sepindex < 0)
                return typeResolver.Resolve(TypeName);
            else
            {
                Type outerType = typeResolver.Resolve(TypeName.Substring(0, sepindex));
                return outerType.Assembly.GetType(outerType.FullName + "+" + TypeName.Substring(sepindex + 1));
            }
        }
    } 
    

    Then, you can use it in your XAML code:

    <Window x:Class="exTibia.Settings"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            Title="Settings"
            Height="350"
            Width="550"
            WindowStyle="ToolWindow"
            ResizeMode="NoResize"
            xmlns:local="clr-namespace:exTibia"
            xmlns:System="clr-namespace:System;assembly=mscorlib"
            Closing="Window_Closing"
            WindowStartupLocation="CenterOwner">
        <Window.Resources>
            <ObjectDataProvider  x:Key="ConstsDirections" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <local:Type2 TypeName="local:Consts+Directions"></x:Type>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>

    Regards,

    Miguel.

    • Marked as answer by panqnik Friday, October 26, 2012 12:44 PM
    Sunday, October 14, 2012 10:15 PM
  • Hi panqnik,

      Welcome to MSDN Forum Support.

     

      You are more likely to get more efficient responses to WPF issues at http://social.msdn.microsoft.com/forums/en/wpf/threads/ where you can contact WPF experts,so I would like to redirect you to appropriate forum for better responses.

      Sincerely,

      Jason Wang



    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 17, 2012 5:51 AM