2

I'm new to WP7 development, so binding is still a little foreign to me. I have a StackPanel that I've set the DataContext with TwoWay binding for editing a given record. But, within the "form" I have a ListPicker that I want to populate with possible values from a lookup table.

Currently I've created a DataTemplate and set the ItemsSource property of the ListPicker. Since the sole TextBlock in the DataTemplate is binding to the ItemsSource data context, how do I then bind the ListPicker's SelectedItem to the StackPanel's data context?

Michael Itzoe
  • 1,949
  • 4
  • 29
  • 49

2 Answers2

1

Your object that you assign to StackPanel's DataContext must expose appropriate properties, for example:

class MyData
{
    public Data { get; set; }
    public Selected { get; set; }
}

Then you need to bind ListPicker's ItemsSource: ItemsSource={Binding Data} and ListPicker's SelectedItem: SelectedItem={Binding Selected}.

If you want ListPicker to react when you change MyData's Selected property you will need MyData class to implement interface IObservable. The same goes for Data property, this collection must inform when it's state changes, so make it of type: ObservableCollection<ElemType>.

saner
  • 38
  • 5
0

Check this: Using the parent's DataContext (WPF - Dynamic Menu Command Binding)

Check all answers and see if one of them helps you.

Community
  • 1
  • 1
Jesus Rodriguez
  • 11,918
  • 9
  • 64
  • 88