2

XAML:

<toolkit:ListPicker x:Name="profession" SelectionChanged="ListPicker_SelectionChanged">
    <toolkit:ListPickerItem Content="Value1" />
</toolkit:ListPicker>

How can I add a key to the ListPickerItem to be used in the C# code?

What I want is similar to HTML's value attribute for the option control.

Example of what I want: (C# code, OFC not working..)

int a = profession.SelectedItem.Key;
Dan Barzilay
  • 4,974
  • 5
  • 27
  • 39

2 Answers2

2

You can use the Tag property.

<toolkit:ListPickerItem Content="Value1" Tag="1"/>
var a = ((ListPickerItem)profession.SelectedItem).Tag;
Alaa Masoud
  • 7,085
  • 3
  • 39
  • 57
0

If you are trying to do this when your app first loads, then in your MainPage.xaml.cs file, include this in your constructor:

public MainPage(){
     InitializeComponent();

     profession.Items.Add("Value2"); // Add this line here
}

Otherwise, put this line wherever you need it.

  • I don't think you understand, but thank you for your answer. what i'm trying to do is add another data (a numeric key) for each `ListPickerItem` (I wrote just 1 up there to make it shorter). So when I use that `ListPickerItem` in my c# I'll be able to get his key as well. i'll add a not working example above in a sec – Dan Barzilay May 29 '13 at 12:33
  • Gotcha. I see you've edited the question to make it much clearer. – DotNetGeekette May 29 '13 at 12:55