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

C# 单击按钮从列表中选择元素。WP8,c#,windows-phone-8,longlistselector,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

我需要一些人帮我做这件事。 我在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;
}                                   
然后,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>
然后,它将我带到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(或编程)的新手,没有做这些事情的经验。
谢谢大家!

您尚未指定项目的类型。所以请提供更多的细节

如果它是一个ObeservableCollection,那么下面的解决方案应该可以工作


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

您可以使用查询字符串将字符串数据传递到
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"
}

参考:

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

命令处理程序将5项绑定到可观察的集合,该集合将被反映

                <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
    }
public-ICommand-LoadSegmentCommand
{
获取{返回新的RelayCommand(onLoadSegmentCommandReceived);}
}
私有void onLoadSegmentCommandReceived(int段)
{
//将数据分配给可观察的集合
}

那么您现在遇到了什么问题?是的,这是一个可观察的集合。public MainViewModel(){this.Items=new observeCollection();}看起来非常合理。让我动手干吧。我会让你知道的。我真是个傻瓜:(我还没能过滤它。不是建议不好。只是我没有能力让它们工作。@GeovanniOrtega你是说
LoadData()
部分?你是怎么实现的?(编辑问题并添加你尝试过的最新代码)是的。我是指那部分。
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"
}
                <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
    }