Category: Silverlight

use Enum as an ItemsSource

Imagine we have an Enum type and we want to use that Enum type to be an ItemsSource of ListBox or ComboBox. Bolow is an example of an Enum type called AirFareBookingStatus

   1: public enum AirFareBookingStatus
   2: {
   3:     AlreadyPaid,
   4:     Cancelled,
   5:     Delayed,
   6:     Deleted,
   7:     InProcessing,
   8:     New
   9: }

What we want to achieve is to populate all the statuses from the Enum to a ListBox or ComboBox.

There is already a solution for this from the Silverlight forum. The idea is to go through all the Field and put them into a IEnumerable of KeyValuepair

   1: IEnumerable<KeyValuePair<string, AirFareBookingStatus>> results;

   2: var x = typeof(AirFareBookingStatus).GetFields().Where(info => info.FieldType.Equals(typeof(AirFareBookingStatus)));

   3: results = from field in x

   4:           select new KeyValuePair<string, AirFareBookingStatus>(field.Name, (AirFareBookingStatus) Enum.Parse(typeof(AirFareBookingStatus), field.Name, false));

Then all we have to do is assign the result to ListBox or ComboBox ItemsSource


   1: lstStatus.ItemsSource = results;

   2: cmbStatus.ItemsSource = results;


That would be good enough for it to work.

But we can even make it more generic by creating a helper function as below

   1: public static class Helper

   2: {

   3:     internal static IEnumerable<KeyValuePair<string, T>> GetEnumList<T>()

   4:     {

   5:         var x = typeof(T).GetFields().Where(info => info.FieldType.Equals(typeof(T)));

   6:         return from field in x

   7:                select new KeyValuePair<string, T>(field.Name, (T) Enum.Parse(typeof(T), field.Name, false));

   8:     }

   9: }

Then, we can call that function for every Enum type we have. For example

   1: lstStatus.ItemsSource = Helper.GetEnumList<AirFareBookingStatus>();

   2: cmbColors.ItemsSource = Helper.GetEnumList<ColorEnum>();

There is still one problem left we have. The item still displays the whole object, so make it display correctly, we need to create DataTemplate resource

   1: <DataTemplate x:Key="KeyValuePairDataTemplate">

   2:     <TextBlock Text="{Binding Key}" />

   3: </DataTemplate>

Then apply that DataTemplate to the ItemTemplate of ListBox or ComboBox as below

   1: <ListBox x:Name="lstStatus"

   2:     ItemTemplate="{StaticResource KeyValuePairDataTemplate}" />

   3:

   4: <ComboBox x:Name="cmbColors"

   5:     ItemTemplate="{StaticResource KeyValuePairDataTemplate}" />

That’s it. Hope that helps.

Continue …

Now, I have run into another problem. Doing that way above is all good, except the name won’t be as flexible. So, I am going to Description attribute to display data instead of the normal name of the Enum. To do that, I’ve changed a bit of my Enum like below

   1: public enum AirFareBookingStatus

   2: {

   3:     [Description("Already Paid")]

   4:     AlreadyPaid,

   5:     [Description("i am cancelled")]

   6:     Cancelled,

   7:     [Description("I got delayed")]

   8:     Delayed,

   9:     [Description("oh man i am about to be deleted")]

  10:     Deleted,

  11:     [Description("who is processing me")]

  12:     InProcessing,

  13:     [Description("I am brand new babe")]

  14:     New

  15: }


Next, I changed a bit in my helper function as well. It’ll get the Description attribute and make it to be Key

   1: internal static IEnumerable<KeyValuePair<string, T>> GetEnumList<T>()

   2:        {

   3:            var x = typeof(T).GetFields().Where(info => info.FieldType.Equals(typeof(T)));

   4:            return from field in x

   5:                   select new KeyValuePair<string, T>(GetEnumDescription(field), (T)Enum.Parse(typeof(T), field.Name, false));

   6:        }

   7:

   8: private static string GetEnumDescription(FieldInfo field)

   9:        {

  10:            DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);

  11:            if (attributes.Length > 0)

  12:            {

  13:                return attributes[0].Description;

  14:            }

  15:            else

  16:            {

  17:                return field.Name;

  18:            }

  19:        }

Finally, we will have something like this

Enum

That’s it. Let me know if you have any comments.

Default Focus Behavior

I would love to share with you guys about Default Focus Behavior which I just created an hour ago because I thought this behavior would be very handy.

With this behavior, we would be able to set focus to any controls when we trigger any control events.

For example, whenever we click the button, we want to textbox got focused, or when we select the Option 1 for the very first time, the textbox will get focused as below image.

defaultfocusbehavior_demo

To set the focus for a control only once, all we have to do is set

IsOnceOff = “True”

Click the icon below for the source code, so you can have a play with it

Hopes this helpful 🙂

Datagrid Missing Binding Error

I have run into this silly mistake, and it took me 2 hours to figure it out 😦

I  am writing it down here for my own reference, and if you ever ran into this problem, hope that this can help you fix your problem quickly.

The problem is I had a datagrid column with the Header but not the Binding, for example

<data:DataGridTextColumn Header=”MyProperty” />

This will cause the below error at runtime

Datagrid Missing Binding Error

To fix it, all I had to do is to put the Binding in it

<data:DataGridTextColumn Header=”MyProperty” Binding={Binding MyProperty} />

Hope this helps

Reblog this post [with Zemanta]

UpperCase Behavior

I was asked to create a TextBox input which has only UpperCase letters. After a bit of research, I have found this article which has inspired me to come up with this solution. As we all know that Behavior is a cool stuff in Silverlight 3, so I decided to use the concept from that article and create a Behavior which detects all the key and uppercase it. Feel free to download the source code and have a play with it.

FocusManager

I’ve read this article about FocusManager. I think it could come handy sometimes when you want to know what element has focus. FocusManager class which can be found in the System.Windows.Input namespace. Below is the sample of how to use itThe FocusManager class has only one static method:

<span class="kwrd">public</span> <span class="kwrd">static</span> Object GetFocusedElement();

Using the FocusManager is easy. Here’s a small example of a possible usage:

var focusedElement = FocusManager.GetFocusedElement() as Control;
if (focusedElement != null)
    Output.Text = focusedElement.Name;

One last note: The FocusManager class is also available in the “normal” .NET Framework 3.0 or later.