在完整模式下选择时,ListPicker所选项目不会更改

时间:2014-07-23 19:01:09

标签: xaml

我有要在ListPicker中显示的类别列表。班级是:

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
}

我将ListPicker完整模式项模板定义为XAML资源:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="FullModeItemTemplate">
            <TextBlock d:DataContext="{Binding}" 
                       Text="{Binding Description}"
                       />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

以及在页面上的XAML中定义的Listpicker:

<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" Background="Transparent">
        <Border Style="{StaticResource ButtonBorderStyle}"
                Background="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>

ItemDescription和Category将被保存到另一个列表中的Question Class Item ...但这与问题无关......

如果类别列表中包含五个或更少的项目,则选择列表将以短模式显示,并且可以选择列表中的项目,并且此选择将反映在控制字段中。这很好。

如果我将类别列表增加到超过五个项目,ListPicker会切换到完整模式,虽然这些项目的显示很好,但是当选择列表中的项目时,控制字段中显示的项目保持不变。 / p>

这是完整模式选择列表中的错误还是我遗漏了什么?

您将获得任何帮助,我们将不胜感激......

1 个答案:

答案 0 :(得分:0)

我在原来的问题中说过:

“ItemDescription和Category将被保存到另一个列表中的Question Class Item ...但这与问题无关......”

虽然这是正确的,但它让我意识到我没有考虑如何在显示包含ListPicker的页面的数据项之前填充它们。此数据填充还包括将ListPicker的SelectedItem参数设置为问题的类别。

我将此数据填充设置为在页面的OnNavigatedTo受保护覆盖​​期间发生。我没有意识到OnNavigatedTo方法也会在从Full Mode Listpicker返回时被触发,当然然后将SelectedItem重置为Question的原始值。为了避免这个问题,我在QuestionId上检查了null,所以OnNavigatedTo只填充初始条目到页面的数据项......问题解决了!