单击按钮时从列表中选择元素。 WP8

时间:2014-09-12 02:35:41

标签: c# windows-phone-8 longlistselector

我需要一些帮助。 我在文件MainViewModel.cs中有一个包含20个元素的MVVM列表:

 public MainViewModel()
    {
        this.Items = new ObservableCollection<ItemViewModel>();
    }
public ObservableCollection<ItemViewModel> Items { get; private set; }

public void LoadData(int part)                                  
{                                   
    if (part == 1)
    {
            this.Items.Add(new ItemViewModel()
            { ID = "0", Title = "Title 0...", Image = "/Images/img001.jpg", Description = "Description 0" });
            this.Items.Add(new ItemViewModel()
            { ID = "1", Title = "Title 1", Image = "/Images/img002.jpg", Description = "Description 1" });
            // And so on from Item 0 to Item 4

     }
     if (part == 2)
     {
            this.Items.Add(new ItemViewModel()
            { ID = "5", Title = "Title 5", Image = "/Images/img006.jpg", Description = "Description 5" });
            this.Items.Add(new ItemViewModel()
            { ID = "6", Title = "Title 6", Image = "/Images/img007.jpg", Description = "Description 6" });
            //And so on from Item 5 to Item 9
      }
      this.IsDataLoaded = true;
}                                   

以及5个按钮列表:

<phone:Panorama Grid.Row="1" Margin="0,3,0,3">
      <phone:PanoramaItem Header="Semanas 1 - 5" HeaderTemplate="{StaticResource MyItemHeaderTemplate}">
            <StackPanel x:Name="Semanas1a5">
                    <Button Content="Semana 1" Click="Button_Click_1"/>
                    <Button Content="Semana 2" Click="Button_Click_2"/>
                    <Button Content="Semana 3" Click="Button_Click_3"/>
                    <Button Content="Semana 4" Click="Button_Click_4"/>
                    <Button Content="Semana 5" Click="Button_Click_5"/>
            </StackPanel>
      </phone:PanoramaItem>
</phone:Panorama>

这就是我需要做的事情。单击每个按钮时,Button1应显示0到4之间的元素,按钮2应显示5到9之间的元素,依此类推。 到目前为止,我能够绑定整个列表,因此每个按钮都会显示完整列表。

这是Button_Click_n处理程序中每个按钮的内容:

 private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/DataBoundApp1;component/MainPage.xaml?dataPart=1", UriKind.Relative));
    //The rest of the butons are the same, just updating dataPart = 2, 3, etc.
    }

然后,MainPage.xaml定义如下:

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Text="Tips Semana 1" Style="{StaticResource WeekHeader}"/>

            <ListBox x:Name="MainLongListSelector" Margin="10,5,0,10" 
                     Background="#50F5F5F5" Foreground="Black"
                     ItemsSource="{Binding Items}"
                     SelectionChanged="MainLongListSelector_SelectionChanged" >

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Image}" Width="100" Height="100" Stretch="Uniform"/>
                            <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Margin="10" 
                                       FontSize="25" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>

和MainPage.cs

public partial class MainPage : PhoneApplicationPage
{
    public string dataPart = string.Empty;
    public int part;
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData(int part);
        }
    }

    private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (MainLongListSelector.SelectedItem == null)
            return;
        NavigationService.Navigate(new Uri("/DataBoundApp1;component/DetailsPage.xaml?selectedIndex=" + (MainLongListSelector.SelectedItem as ItemViewModel).ID, UriKind.Relative));
        MainLongListSelector.SelectedItem = null;
    }

}

然后,它将我带到App.cs,我必须更新LoadData以接收参数,它结束如下:

private void Application_Activated(object sender, ActivatedEventArgs e)

    public int part;
    {
        // Ensure that application state is restored appropriately
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData(int part);
        }
    }

有了这一切,点击任何按钮,应用程序退出(没有例外,没有错误显示,只是退出)。任何人都可以帮助我吗? 我是WP8的新手(或编程)并没有做这些事情的经验。 谢谢!

3 个答案:

答案 0 :(得分:0)

您尚未指定Items的类型。所以请详细说明一下。

如果它是ObeservableCollection而不是以下解决方案应该有效。

您可以将整个列表作为单独的全局列表。在每个按钮上单击,您可以在全局列表中应用过滤器并根据该列表创建项目列表。我认为这应该有用。

答案 1 :(得分:0)

您可以使用查询字符串将字符串数据传递给MainPage.xaml

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    NavigationService.Navigate(new Uri("/DataBoundApp1;component/MainPage.xaml?dataPart=1", UriKind.Relative));
}

然后在MainPage.xaml中读取查询字符串并相应地加载数据:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string dataPart = string.Empty;
    if (NavigationContext.QueryString.TryGetValue("dataPart", out dataPart)) 
    {
        int part = int.Parse(dataPart);
        LoadData(int part);
    }
}

public void LoadData(int part)                                  
{                                   
    //load relevant items according to parameter "part"
}

供参考:How to pass values (parameters) between XAML pages?

答案 2 :(得分:0)

我建议您将所有4个按钮的Button Click事件绑定到命令,并将参数传递为(1,2,3,4)以指示要加载的段。

Command Handler会将5个项目绑定到Observable Collection,这将反映出来。

                <Button Content="Btn1">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <i:InvokeCommandAction Command="{Binding LoadSegmentCommand}" CommandParameter="{Binding SegmentNumber}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button> 

在视图模型中

    public ICommand LoadSegmentCommand
    {
        get { return new RelayCommand<int>(OnLoadSegmentCommandCommandReceived); }
    }

    private void OnLoadSegmentCommandCommandReceived(int segment)
    {
        // Assign the data to Observable Collection
    }