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

Windows phone 7 LongListSelector中的ListPicker在滚动后不保留值,windows-phone-7,longlistselector,listpicker,Windows Phone 7,Longlistselector,Listpicker,我正在开发一个包含LongListSelector的WP7应用程序。在ItemTemplate中有一个ListPicker。我从ListPicker中为LongListSelector中的第一项选择一个值,然后从ListPicker中为第二项选择一个值。如果我随后向下滚动页面并再次返回顶部,我在ListPicker中为第一项选择的值将被重置(SelectedIndex=0) 我将代码放入LongListSelector的链接和取消链接事件中,以写入输出窗口,我发现当第一个项目取消链接(由于向下滚

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

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

Im使用ObsableCollection并为我的对象实现INPC接口,当ListPicker选择更改时,ViewModel将更新

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

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);
    }

}
public部分类主页:PhoneApplicationPage
{
私有TestItemViewModel_vm;
公共测试视图模型视图模型
{
收到
{
如果(_vm==null)
_vm=新的TestItemViewModel();
返回虚拟机;
}
专用设备
{
_vm=值;
}
}                
//建造师
公共主页()
{
初始化组件();
this.lstOutstandingInstances.Link+=新事件处理程序(lstOutstandingInstances\u Link);
this.lstOutstandingInstances.Unlink+=新事件处理程序(lstOutstandingInstances\u Unlink);
this.DataContext=this.ViewModel;
}
void lstu链接(对象发送方,链接UnlinkEventArgs e)
{
var item=e.ContentPresenter.Content作为TestItem;
WriteLine(“ItemNumber{0}={1}”、item.ItemNumber、item.Result的链接事件);
}
void lstandings\u Unlink(对象发送方,linkunlinkventargs e)
{
var item=e.ContentPresenter.Content作为TestItem;
WriteLine(“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));
        }
    }

}
公共类TestItemViewModel:BaseINPC
{
公共可观测集合测试集合
{
收到
私人设置;
}
//建造师
公共TestItemViewModel()
{
this.TestItemCollection=新的ObservableCollection();
CreateTestData(20);
}
公共void CreateTestData(int totalItems)
{
//为长列表选择器创建测试数据。
对于(int i=1;i
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;
        }
    }

}