WP7列表框绑定到第二个可观察集合自动选择错误

时间:2012-06-01 07:25:10

标签: c# windows-phone-7 data-binding listbox

我有一个绑定到服务的列表框。它加载正常,允许我选择一个项目并移动到第二页。

第二页从同一服务加载一个新的可观察集合,并使用上一页中选定的项目加载带有数据过滤器的新列表框。

此第二个列表框允许进一步选择基于此选择加载详细信息页面。我遇到的错误是第二个列表框加载并自动选择列表中的第一个项目。按下后退按钮后,它会加载第二个列表框而不进行任何选择。

有人可以告诉我如何在第二个列表框中停止自动选择吗?代码详情如下。

包含发件人的第一个列表框代码

void ServiceReference1Client_GetMensleaderListCompleted(object sender, GetMensLeaderListCompletedEventArgs e)
    {
        if (e.Error != null) { MensHeading.Text = "Connect to Data"; return; }

        ObservableCollection<Mens_Leaders> mensLeaders = e.Result;

        this.mensRankings.ItemsSource = mensLeaders;

    }

    private void mensRankings_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (mensRankings.SelectedItem != null)
        {
            Mens_Leaders SelectedPlayer = mensRankings.SelectedItem as Mens_Leaders;
            _SelectedMensPlayer = (sender as ListBox).SelectedItem;
            NavigationService.Navigate(new Uri("/Pages/PlayerProfile.xaml", UriKind.Relative));
            FrameworkElement root2 = Application.Current.RootVisual as FrameworkElement;
            root2.DataContext = _SelectedMensPlayer;
        }
    }

加载第二个列表的页面

private void LoadPlayerList()
    {
        Service1Client ServiceReference1Client = new Service1Client();

        ServiceReference1Client.GetTournamentListCompleted += new EventHandler<GetTournamentListCompletedEventArgs>(ServiceReference1Client_GetTournamentListCompleted);
        ServiceReference1Client.GetTournamentListAsync();

        FrameworkElement root2 = Application.Current.RootVisual as FrameworkElement;
        var currentPlayer = root2.DataContext as ATP_Tennis_App.TennisService.Mens_Leaders;
        _SelectedPlayer = currentPlayer;
        _selectedPlayerTournamentsWon = currentPlayer.Name;

    }
void ServiceReference1Client_GetTournamentListCompleted(object sender, GetTournamentListCompletedEventArgs e)
    {
        if (e.Error != null) { return; }

        ObservableCollection<Tournaments> tournamentList = e.Result;

        viewSource = new CollectionViewSource();
        viewSource.Filter += TournamentWin_Filter;
        viewSource.Source = tournamentList;
        this.listBox1.ItemsSource = viewSource.View;
    }

    void TournamentWin_Filter(object sender, FilterEventArgs e)
    {
        if (e.Item != null)
            e.Accepted = ((Tournaments)e.Item).Prev_Male_Winner.Contains(_selectedPlayerTournamentsWon);
    }

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {

            Tournaments selectedTournament1 = listBox1.SelectedItem as Tournaments;

            _SelectedItemTournament = (sender as ListBox).SelectedItem;
            NavigationService.Navigate(new Uri("/Pages/TournamentDetailsPanorama.xaml", UriKind.Relative));
            FrameworkElement root3 = Application.Current.RootVisual as FrameworkElement;
            root3.DataContext = _SelectedItemTournament;

        }
        else
        {
            return;
        }
    }

2 个答案:

答案 0 :(得分:1)

在列表框中,将IsSynchroziedWithCurrentItem设置为False。这解决了这个问题。

答案 1 :(得分:0)

我知道这是一个肮脏的解决方案,但你可以设置listBox1.SelectedIndex = -1