在完整模式下选择时,ListPicker所选项目不会更改 [英] ListPicker selected item not changing when selected in Full Mode

查看:60
本文介绍了在完整模式下选择时,ListPicker所选项目不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要在ListPicker中显示的类别列表。该课程为:

I have List of Categories that I want to display in a ListPicker. The Class is:

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将ListPicker完整模式项模板定义为XAML资源:


I have a ListPicker Full Mode Item Template defined as a XAML Resource:

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



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


and a ListPicker for it defined in XAML on the Page:

<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>





ItemDescription和Category是t o保存到另一个列表中的问题类项目...但这与问题无关...



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



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



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



您可以给予任何帮助...



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...

推荐答案

在我原来的问题中我说:



ItemDescription和Category将被保存到另一个列表中的问题类项目......但这与问题无关...... br />


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



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


这篇关于在完整模式下选择时,ListPicker所选项目不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆