Search code examples
xamlbindingsilverlight-toolkit

How do I refer to simple strings bound to a Silverlight Toolkit ListPicker in a DataTemplate?


I am styling the ListPicker with a DataTemplate. All the examples I have read assume that the ListItems are bound complex objects and so can refer to properties on the objects using the usual Binding Property syntax. e.g.

this.myListPicker.Items.Add(new Profile() { Name = "Joe", 
    Occupation="Button pusher" });

and in the XAML,

<DataTemplate>
    <StackPanel>
        <TextBox Text="{Binding Name}" />
        <TextBox Text="{Binding Occupation}" />
    </StackPanel>
</DataTemplate>

However my ListPickers are bound to a List of simple strings and there does not seem to be a property on the string that actually refers to the string.

How do I get a handle to the string inside the DataTemplate so that I can assign them to a e.g. TextBox in the template?


Solution

  • In the example code you show, you do actually have a "complex" object, a Profile, and the binding you specify should work.

    If you really just have strings as the items, then the binding in your ItemTemplate would look like:

    <TextBox Text="{Binding }" />
    

    The relevant detail is that all ItemsControls (and ListPicker seems to be one) set the DataContext of the view of each item to the item. The binding syntax, above, means "bind to the source itself", and, unless otherwise specified (like with Source or RelativeSource, etc.), the source of any binding is the DataContext.

    MSDN has more details here.