WP7 - accessing selected item in listbox when listbox is within an itemtemplate
I have a Pivot item template that includes a listbox
<controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItemHeaders}"开发者_运维技巧 Title="CLASS TIMETABLE" >
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}"/>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox x:Name="Events" ItemsSource="{Binding allEventItems}" ItemTemplate="{StaticResource EventDisplay2}"/>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
In the code behind I want to access the selectedItem of that listbox but I cannot 'get' to the listbox as such because ity is (presumably) within the template
i.e
this.NavigationService.Navigate(new Uri("/View/EventEdit.xaml?selectedEvent=" + Events.SelectedItem, UriKind.Relative));
The Events listbox is not being recognised.
Assuminh I can pass get the object and pass it through as a parameter, what code can I use to retrieve it
I know its starts with protected override void OnNavigatedTo(NavigationEventArgs e) { if (NavigationContext.QueryString.ContainsKey("SelectedEvent")) {
But I am unsure of the syntax/code to extract out the object from the parameters
Appreciate how I can get the selectedItem from this listbox and the code to get the object being passed through
- thanks
Rather than attempt to access the ListBox, you could use the SelectionChanged
event to be told when the value changes:
<ListBox x:Name="Events"
ItemsSource="{Binding allEventItems}"
ItemTemplate="{StaticResource EventDisplay2}"
SelectionChanged="Event_SelectionChanged" />
And then in your code behind:
private void Event_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.selectedEvent = (EventItem)e.AddedItems[0];
}
You can access the value using NavigationContext.QueryString["selectedEvent"]
, but you can only store strings in navigation query strings. If your listbox is currently bound to objects, you'll need to select a key and then find that event from the second page using that key.
精彩评论