This is a migrated thread and some comments may be shown as answers.

ListPicker for Enum [Flags] ?

3 Answers 347 Views
ListPicker
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Martin
Top achievements
Rank 1
Martin asked on 21 Jun 2012, 08:58 AM

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,
}

3 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 21 Jun 2012, 08:59 AM
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 >>

0
Martin
Top achievements
Rank 1
answered on 21 Jun 2012, 01:19 PM

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.

0
Todor
Telerik team
answered on 22 Jun 2012, 01:55 PM
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 >>

Tags
ListPicker
Asked by
Martin
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Martin
Top achievements
Rank 1
Todor
Telerik team
Share this question
or