Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
More...


Resources » .NET programming » WPF

Binding Enum With Combobox in wpf


Posted Date:     Category: WPF    
Author: Member Level: Gold    Points: 60


It is very easy and efficient way to bind combo box in wpf some people will use reflection with Description Attribute which is again performance issue that is as per size of our enum it will take more space in memory. This will bind Enums values with combobox in different ways Simple Enum Binding Binding With Helper Class Binding With Dicitonary



 


It is very easy and efficient way to bind combo box in wpf some people will use reflection with Description Attribute which is again performance issue that is as per size of our enum it will take more space in memory. And this is good practice to ignore use of reflection if we can do the things without reflection.

There are several way to implement this :

Simple Way to Bind Enum With Combobox


Object Data Provider we will put in Windows resources here we have to add <> tags while using in code or application.


//XMAL File code
//StaticResource
Object Data Provider MethodName="GetValues" ObjectType="{x:Type
sys:Enum}" x:Key="rv"
Object Data Provider.MethodParameters

x:Type TypeName="xx:TheWeek"
Object Data Provider.MethodParameters
Object Data Provider
//StaticResource

ComboBox ItemsSource="{Binding Source={StaticResource rv}}" IsSynchronizedWithCurrentItem="true"

//Code Behind File

public enum TheWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}


Binding Enum's Description With Combobox



IF there is space between the enum's value like Sunday [Day of Sun] Then we Can use this approach also to show enum description


//Xmal Code
//Resources
ObjectDataProvider MethodName="GetDescription" ObjectType="{x:Type sys:Enum}" x:Key="rg"
ObjectDataProvider.MethodParameters
ObjectDataProvider.MethodParameters
ObjectDataProvider

StackPanel
ComboBox
ItemsSource="{Binding Source={xx:Enumeration {x:Type xx:TheWeek}}}"
DisplayMemberPath="Description"
SelectedValue="{Binding CurrentStatus}"
SelectedValuePath="Value"
StackPanel

//Code Behind Helper Class which we need to show description attribute of enum
public class EnumerationExtension : MarkupExtension
{
private Type _enumType;


public EnumerationExtension(Type enumType)
{
if (enumType == null)
throw new ArgumentNullException("enumType");

EnumType = enumType;
}

public Type EnumType
{
get { return _enumType; }
private set
{
if (_enumType == value)
return;

var enumType = Nullable.GetUnderlyingType(value) ?? value;

if (enumType.IsEnum == false)
throw new ArgumentException("Type must be an Enum.");

_enumType = value;
}
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
var enumValues = Enum.GetValues(EnumType);

return (
from object enumValue in enumValues
select new EnumerationMember
{
Value = enumValue,
Description = GetDescription(enumValue)
}).ToArray();
}

private string GetDescription(object enumValue)
{
var descriptionAttribute = EnumType
.GetField(enumValue.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;


return descriptionAttribute != null
? descriptionAttribute.Description
: enumValue.ToString();
}

public class EnumerationMember
{
public string Description { get; set; }
public object Value { get; set; }
}
}


Binding Enum's Description Using Dictionary With Combobox in WPF



// By taking list or dictionary we can bind enum values with static resources

//Xaml Code
ComboBox ItemsSource="{Binding Source={StaticResource theWeekList },
Path= TheWeekList }"SelectedIndex="0" DisplayMemberPath="Value"
SelectedValuePath="Key"

public enum TheWeek
{

Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}

_theWeekList = new Dictionary()
{
{TheWeek.Friday, "First Day"},
{TheWeek.Monday, "Second Day"},
{TheWeek.Saturday, "Third Day"},
{TheWeek.Sunday, "Fourth Day"},
{TheWeek.Thursday, "Fifth Day"},
{TheWeek.Tuesday, "Six Day"},
{TheWeek.Wednesday, "Seven Day"},
};

private static Dictionary _theWeekList;
public static Dictionary TheWeekList
{
get
{
return _theWeekList;
}
}



Note : There are many way to do this task in WPF and this is just one of many ways you can achieve the same.





Did you like this resource? Share it with your friends and show your love!


Responses to "Binding Enum With Combobox in wpf"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Windows Presentation Foundation Fundamentals Concepts
    Previous Resource: How to Implement MVVM
    Return to Resources
    Post New Resource
    Category: WPF


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Simple Enum Binding With Combox  .  Enum Description Binding With Combox  .  Enum Binding with Dicitionary  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.