I have a Menu object (set as the DataContext) which contains many Options (Menu.Options), which contains a Name (Option.Name) and many Options (Option.Options). The collections are all of type ObservableCollection<T>.
The Menu is loaded from an XML file, so the amount of Options and Values can vary.
To help visualise, here is the relevant XAML:
<ListBox ItemsSource="{Binding Path=Options}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<!-- Title -->
<TextBlock Text="{Binding Path=Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<!-- Selection -->
<toolkit:ListPicker ItemsSource="{Binding Path=Options}" SelectionChanged="ListPicker_SelectionChanged">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a method ListPicker_SelectionChanged on the SelectionChanged event, in which I want to somehow mark the current selection in the databound model. I need to do it using just the parameters supplied to the method, as each ListPicker is generated at runtime. So I can’t be specifying actual control names (as far as I’m aware anyway).
I can see two possible options:
1) To have a CurrentSelection inside Menu.Option, where I can put a reference to the last selected item for that ListPicker
2) To have a Selected attribute on the Option.Option. Downside here though is making sure all elements are deselected when a new one is selected.
I’ve tried browsing the object tree of sender, but anything I find, such as ItemsHost, is inaccessible (private/protected).
Is there any way I can achieve a solution?
Option 1 is the better one, use TwoWay binding on the ListPicker.SelectedItem.
Here’s the Code Behind