Search code examples
windows-phone-7silverlight-toolkit

I fail to understand databinding in WP7


In android this is simple and straightforward. In WP7 it's not (at least for me). What I'm trying to achieve: I want to have a ListPicker:

<toolkit:ListPicker>

I want to poupulate it with Strings (same way you do with xml arrays for spinners in android)

I want the string to be displayed to the user as an item inside the picker. It would also be good if the solution to this supported internatiolization. I tried creating Resources with a list of strings but couldn't figure out how to "bind" it to the control. I don't want the string values to be hardcoded in .cs files.


Solution

  • The 'standard' way to populate a ListPicker control is to set its ItemsSource property to the collection which you wish to render. The ListPicker has an ItemTemplate which details how each item is rendered. If you do not specify an ItemTemplate, the default template will be used an toString() invoked on each of the items in your collection in order to render them.

    See this tutorial for more details:

    ListPicker for WP7 in depth

    So a simple example would be:

    <toolkit:ListPicker x:Name="myList"/>
    

    And in code-behind:

    myList.ItemsSource = new List<string>() {
      "one", "two", "three"
    };