0

我在让长列表选择器加载我的数据时遇到一些问题,我一直无法找到解决这个问题的方法。

这是我的 xaml:

<phone:LongListSelector x:Name="animeList" 
                        Margin="0,0,-12,0" 
                        ItemsSource="{Binding Animes}"
                        Tap="AnimeList_OnTap">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17">
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}">
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu IsZoomEnabled="false">
                            <toolkit:MenuItem Header="Add as favorit" Click="AddFavorite" />
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

我的视图模型很简单:

public ObservableCollection<AnimeItemViewModel> _animes { get; set; }
public ObservableCollection<AnimeItemViewModel> Animes
{
    get { return _animes; }
    set
    {
        if (value != _animes)
        {
            _animes = value;
            NotifyPropertyChanged("Animes");
        }
    }
}

以及我如何加载数据:

public MainPage()
{
    InitializeComponent();

    DataContext = App.ViewModel;
    this.Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
    ObservableCollection<AnimeItemViewModel> _animes = new ObservableCollection<AnimeItemViewModel>();
    foreach (var i in App.AnimeList.List)
        _animes.Add(new AnimeItemViewModel() { AId = i.AId, Name = i.Name });
    App.ViewModel.Animes = _animes;
}

最后只是为了表明列表中有数据 在此处输入图像描述

更新:我也有搜索功能,如果我输入搜索文本,长列表会更新,但由于某种原因我无法滚动

private void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        ObservableCollection<AnimeItemViewModel> _animes = new ObservableCollection<AnimeItemViewModel>();
        foreach (var i in App.AnimeList.List)
            if (string.IsNullOrWhiteSpace(SearchTextBox.Text) || i.Name.ToLower().Contains(SearchTextBox.Text.ToLower()))
                _animes.Add(new AnimeItemViewModel() { AId = i.AId, Name = i.Name });
        App.ViewModel.Animes = _animes;
    }
}
4

1 回答 1

1

尝试将加载代码放入OnNavigatedTo. 至于滚动问题 - 为StackPanel. 让我知道它是否有效。

于 2013-07-20T21:20:19.840 回答