Question

This is an interesting problem for me, I have a ListPicker in my view, but have used a StackPanel within the DataTemplate of the ListPicker so I can avoid the problem of the ListPicker SelectionChanged event being called when the page is NavigatedTo. With this implementation, I am having trouble getting the item that was selected in the ListPicker in the View. I need to get the lower case version of name (either "light" or "dark") of the item that was selected in the ListPicker.

MainPage.xaml

<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="PickerItemTemplate"> 
       <StackPanel tap="stk_Tap"> 
            <TextBlock Text="{Binding Name}"/> 
        </StackPanel> 
</DataTemplate>
</phone:PhoneApplicationPage.Resources>

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme"
            ItemTemplate="{StaticResource PickerItemTemplate}"/>

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    themeList = new List<TestApp.Common.Theme>();
    themeList.Add(new TestApp.Common.Theme() { Name = "Darker", name = "dark" });
    themeList.Add(new TestApp.Common.Theme() { Name = "Lighter", name = "light" });
    ThemeListPicker.ItemsSource = themeList;
}

private void stk_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (ThemeListPicker.SelectedIndex != -1)
    {
        //Need to get the current ThemeListPicker's 'name'
        var selectedItem1 = (sender as StackPanel).DataContext as ListPicker;

        //use selectedItem1

    }
}
Was it helpful?

Solution

DataContext of that StackPanel is TestApp.Common.Theme.

So, using this should work

var selectedItem1 = (sender as StackPanel).DataContext as TestApp.Common.Theme;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top