Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have List of Categories that I want to display in a ListPicker. The Class is:
C#
public class Category : INotifyPropertyChanged, INotifyPropertyChanging
{
    private int _id;
    public int Id
    {
        get { return _id; }
        set
        {
            if (_id == value) return;
            NotifyPropertyChanging();
            _id = value;
            NotifyPropertyChanged();
        }
    }

    private string _description;
    public string Description
    {
        get { return _description; }
        set
        {
            if (_description == value) return;
            NotifyPropertyChanging();
            _description = value;
            NotifyPropertyChanged();
        }
    }

    #region INotifyPropertyChanging and INotifyPropertyChanged Members
    public event PropertyChangingEventHandler PropertyChanging;
    /// <summary>
    /// Used to notify the data context that a property is about to change...
    /// </summary>
    private void NotifyPropertyChanging([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanging != null) PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Used to notify the data context that a property has changed...
    /// </summary>
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

I have a ListPicker Full Mode Item Template defined as a XAML Resource:
C#
<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="FullModeItemTemplate">
            <TextBlock d:DataContext="{Binding}" 
                       Text="{Binding Description}"
                       />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

and a ListPicker for it defined in XAML on the Page:
C#
<phone:PhoneApplicationPage
    x:Class="QuestionPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeMedium}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed.-->
    <Grid x:Name="LayoutRoot" removed="Transparent">
        <Border Style="{StaticResource ButtonBorderStyle}"
                removed="Blue"
                BorderBrush="White"
                >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <!--TitlePanel contains the name of the app and page title.-->
                <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="0,10,0,0">
                    <TextBlock x:Name="ApplicationTitle" 
                        Text="pagetitle" 
                        Style="{StaticResource PhoneTextLargeStyle}"/>
                    <TextBlock x:Name="PageTitle" 
                        Text="subtitle" 
                        Style="{StaticResource  PhoneTextExtraLargeStyle}"/>
                </StackPanel>
                <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,15">
                    <TextBlock Text="question" Margin="20,5,0,0"/>
                    <TextBox x:Name="ItemDescription"/>
                    <TextBlock Text="category" Margin="20,0,0,-10"/>
                    <toolkit:ListPicker x:Name="CategoriesListPicker"
                                        DataContext="{Binding}" ItemsSource="{Binding Categories}"
                                        DisplayMemberPath="Description"
                                        FullModeHeader="Categories:"
                                        FullModeItemTemplate="{StaticResource FullModeItemTemplate}"
                                        CacheMode="BitmapCache"
                                        >
                    </toolkit:ListPicker>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>


</phone:PhoneApplicationPage>


The ItemDescription and Category are to be saved to a Question Class Item in another list... but this is not relevant to the problem...

If the Category List has five or less items in it, the selection list is displayed in short mode and items from the list can be selected and this selection is reflected in the control field. This is fine.

If I increase the Categories List to over five items, the ListPicker switches to full mode, and although the display of these is fine, when an item from the list is selected, the item shown in the control field remains unchanged.

Is this a bug in the Full Mode selection list or am I missing something?

Any help you can give will be appreciated...
Posted

1 solution

In my original question I stated:

"The ItemDescription and Category are to be saved to a Question Class Item in another list... but this is not relevant to the problem..."

Although this is correct, it made me realise I'd not taken into consideration how the data items for the page containing the ListPicker are populated before it is displayed. This data population also includes setting the SelectedItem parameter for the ListPicker to the Category for the Question.

I set this data population to happen during the OnNavigatedTo protected override for the page. I'd not realised that the OnNavigatedTo method also gets triggered on return from the Full Mode Listpicker which of course then reset the SelectedItem to the original value for the Question. To avoid this problem, I put in a check for null on the QuestionId so OnNavigatedTo only populates the data items on initial entry to the page... problem solved!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900