LongListSelector中的ListPicker在滚动后不保留值

时间:2013-08-20 00:56:37

标签: windows-phone-7 longlistselector listpicker

我正在开发一个包含LongListSelector的WP7应用程序。在ItemTemplate中有一个ListPicker。我从ListPicker中为LongListSelector中的第一个项目选择一个值,然后从ListPicker中为第二个项目选择一个值。如果我然后向下滚动页面并再次返回顶部,我将在ListPicker中为第一项选择的值将被重置(SelectedIndex = 0)。

我已将代码放入LongListSelector的Link和Unlink事件中以写入输出窗口,我发现当第一项取消链接时(由于向下滚动页面),ListPicker值是我选择但是as一旦链接事件触发(由于向上滚动页面)第一项,该值就会重置。

我使用ObserableCollection并为我的对象实现INPC接口,并在更改ListPicker选择时更新ViewModel。

如何在滚动LongListSelector期间确保ListPickers中的值保留?

MainPage.xaml中

<phone:PhoneApplicationPage 
x:Class="LongListSelector.MainPage"
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"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="5000"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="occurrenceItemTemplate">
        <Grid>
            <StackPanel Orientation="Vertical" Margin="5,0,0,0" >
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Name="tbkEventDateTime" Grid.Row="0" Grid.Column="0"
                                        Text="{Binding ItemNumber}" 
                                        Style="{StaticResource PhoneTextSmallStyle}" />

                    <!--<TextBox Name="txtEventDateTime" Grid.Row="1" Grid.Column="0"
                                        Text="{Binding Result}" />-->

                    <toolkit:ListPicker Name="lpkResult" Margin="12,0,12,12"
                                    Grid.Row="1" Grid.Column="0"
                                    ItemsSource="{Binding TestItemResultList, Mode=OneTime}"
                                    SelectedIndex="{Binding Result, Mode=TwoWay}"
                                    CacheMode="BitmapCache">
                        <toolkit:ListPicker.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Text}" />
                            </DataTemplate>
                        </toolkit:ListPicker.ItemTemplate>
                    </toolkit:ListPicker>
                </Grid>
            </StackPanel>
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" VerticalAlignment="Stretch">
        <toolkit:LongListSelector x:Name="lstOutstandingOccurrences" 
                Margin="0,12,0,0" Padding="0,0,0,24"
                ItemTemplate="{StaticResource occurrenceItemTemplate}"
                ItemsSource="{Binding TestItemCollection, Mode=TwoWay}"
                IsFlatList="True" ShowListHeader="False" >
        </toolkit:LongListSelector>
    </Grid>
</Grid>

MainPage.xaml.cs中

public partial class MainPage : PhoneApplicationPage
{
    private TestItemViewModel _vm;
    public TestItemViewModel ViewModel
    {
        get
        {
            if (_vm == null)
                _vm = new TestItemViewModel();

            return _vm;
        }
        private set
        {
            _vm = value;
        }
    }                

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        this.lstOutstandingOccurrences.Link += new EventHandler<LinkUnlinkEventArgs>(lstOutstandingOccurrences_Link);
        this.lstOutstandingOccurrences.Unlink += new EventHandler<LinkUnlinkEventArgs>(lstOutstandingOccurrences_Unlink);

        this.DataContext = this.ViewModel;
    }

    void lstOutstandingOccurrences_Link(object sender, LinkUnlinkEventArgs e)
    {
        var item = e.ContentPresenter.Content as TestItem;
        Debug.WriteLine("Link event for ItemNumber {0} = {1}", item.ItemNumber, item.Result);
    }

    void lstOutstandingOccurrences_Unlink(object sender, LinkUnlinkEventArgs e)
    {
        var item = e.ContentPresenter.Content as TestItem;
        Debug.WriteLine("Unlink event for ItemNumber {0} = {1}", item.ItemNumber, item.Result);
    }

}

TestItemViewModel.cs

public class TestItemViewModel : BaseINPC
{
    public ObservableCollection<TestItem> TestItemCollection
    {
        get;
        private set;
    }

    // Constructor
    public TestItemViewModel()
    {
        this.TestItemCollection = new ObservableCollection<TestItem>();
        CreateTestData(20);
    }

    public void CreateTestData(int totalItems)
    {
        //create test data for long list selector.
        for (int i = 1; i <= totalItems; i++)
        {
            this.TestItemCollection.Add(new TestItem(i, 0));
        }
    }

}

模型

public class ListHelperListItem
{
    public int Value { get; set; }
    public string Text { get; set; }
}

public class TestItem : BaseINPC
{
    public TestItem(int ItemNumber, int Result)
    {
        this.ItemNumber = ItemNumber;
        this.Result = Result;
    }

    public int ItemNumber { get; set; }

    private int _Result;
    public int Result
    {
        get
        {
            return _Result;
        }
        set
        {
            //if statement is for debugging purposes only.
            if (this.ItemNumber == 1)
            {
                _Result = value;
            }

            _Result = value;
            RaisePropertyChanged("Result");
        }
    }

    private List<ListHelperListItem> _TestItemResultList;
    public List<ListHelperListItem> TestItemResultList
    {
        get
        {
            _TestItemResultList = new List<ListHelperListItem>();
            _TestItemResultList.Add(new ListHelperListItem { Value = 0, Text = " " });
            _TestItemResultList.Add(new ListHelperListItem { Value = 1, Text = "Yes" });
            _TestItemResultList.Add(new ListHelperListItem { Value = 2, Text = "No" });
            _TestItemResultList.Add(new ListHelperListItem { Value = 3, Text = "Ignore" });
            return _TestItemResultList;
        }
    }

}

感谢您的帮助!

0 个答案:

没有答案