ListPicker for Enum [Flags] ?

4 posts, 0 answers
  1. Martin
    Martin avatar
    8 posts
    Member since:
    Oct 2011

    Posted 21 Jun 2012 Link to this post

    With the recent introduction of multi list picker I have an idea for another feature. I currently use Enum with [Flags] attribute for storing data user selects. It will be great to have the possibility to edit this Enum value using ListPicker with multiselect.

    Typical use:

    [Flags]
    public enum Recurrence
    {
        Monday = 0x1,
        Tuesday = 0x2,
        Wednesday = 0x4,
        Thursday = 0x8,
        Friday = 0x10,
        Saturday = 0x20,
        Sunday = 0x40,
    }

  2. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 21 Jun 2012 Link to this post

    Hi,

    Can you please elaborate a little bit more on what do you mean by "edit the enum value"?

    Regards,
    Valentin.Stoychev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Martin
    Martin avatar
    8 posts
    Member since:
    Oct 2011

    Posted 21 Jun 2012 Link to this post

    Well imagine I got this property and I set it to initial value

    public Recurrence AlarmRecurrence { get; set; }
    AlarmRecurrence = Recurrence.Monday | Recurrence.Wednesday | Recurrence.Sunday;
     

    I'd like to have ListPicker for setting the value of my property AlarmRecurrence where user can select the target days using checkboxes just like in the built-in Windows Phone alarm.

    Currently the ListPicker works only on list of possible values, selecting one, or on list of possible values, selecting list of values.

  5. Todor
    Admin
    Todor avatar
    778 posts

    Posted 22 Jun 2012 Link to this post

    Hello Martin,

    So, you want your ItemsSource to be the enumeration. Here is how you can convert an enumeration to a List<string>:

    List<string> items = new List<string>();
    foreach (var x in typeof(Recurrence).GetFields())
    {
        if (x.IsLiteral)
        {
            items.Add(x.Name);
        }
    }
    return items;

    Then when the SelectionChanged event occurs you can get the result by something similar to this:

    Recurrence result = GetRecurrence(this.listPicker.SelectedItems[0]);
    for (int i = 1; i < this.listPicker.SelectedItems.Count; i++)
    {
        result |= GetRecurrence(this.listPicker.SelectedItems[i]);
    }
    return result;

    Where GetRecurrence is converting back a string to Recurrence:

    private ListPickerExample.ViewModels.MainViewModel.Recurrence GetRecurence(object source)
    {
        switch (source.ToString().ToLower())
        {
            case "monday": return Recurrence.Monday;
            case "tuesday": return Recurrence.Tuesday;
            case "wednesday": return Recurrence.Wednesday;
            case "thursday": return Recurrence.Thursday;
            case "friday": return Recurrence.Friday;
            case "saturday": return Recurrence.Saturday;
            case "sunday": return Recurrence.Sunday;
            default: return Recurrence.Monday;
        }
    }

    I hope this helps. Let me know if you need additional assistance.

    Kind regards,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top