重新加载相同页面但在列表框选择更改事件Windows Phone 7更新

时间:2014-02-19 21:31:07

标签: c# windows-phone-8

以下是我想看到的示例:http://www.mywork.com.au/blog/how-to-create-a-simple-facebook-style-sidebar-menu-using-only-css-and-minimal-javascript/

因为我在列表框中显示category_headings(它们是新闻源,消息等)。当用户选择一个类别并且更改了事件时,我想要的是它然后它应该在同一页面上显示该类别的相关细节。

这是我的代码Class1.cs

   public class RootObject
    {
        public string category_id { get; set; }
        public string category_heading { get; set; }
        public string detail { get; set; }
    }

MainPage.xaml.cs中的代码

  namespace testApp
 {

 public partial class mainpage : PhoneApplicationPage
 {
    private bool _isSettingsOpen = false;

     RootObject obj = new RootObject();

     public mainpageUI()
    {
        InitializeComponent();
        Loaded += mainpage;
    }       

    public void mainpage(object sender, RoutedEventArgs e)
    {
        WebClient webClient = new WebClient();
        webClient.DownloadStringAsync(new Uri("JSON STRING"));
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
    }


    public void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        RootObject[] jobj = JsonConvert.DeserializeObject<RootObject[]>(e.Result);       


        listBox1.ItemsSource = jobj;

    }

    **private void on_click(object sender, SelectionChangedEventArgs e) //listbox selectionchanged event
    {
        //what should I do here so that every time user will select different category it will display details   on same page.

    }**

  private void button1_Click(object sender, EventArgs e)  //Code for event which opens Facebook like menu pane
    {

        if (_isSettingsOpen)
        {
            VisualStateManager.GoToState(this, "SettingsClosedState", true);
            _isSettingsOpen = false;
        }
        else
        {
            VisualStateManager.GoToState(this, "SettingsOpenState", true);
            _isSettingsOpen = true;
        }

    }        
  }
}

1 个答案:

答案 0 :(得分:0)

要根据用户选择过滤项目,请先将数据保存到属性。然后,当选择发生时,过滤属性并重新附加。请注意,无需重新加载页面,只需更改控件的绑定即可使用已过滤的新列表。

private RootObject[] InitialFeed { get; set; } 


public void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
   InitialFeed = JsonConvert.DeserializeObject<RootObject[]>(e.Result);       

  listBox1.ItemsSource = InitialFeed ;

}

private void on_click(object sender, SelectionChangedEventArgs e) //listbox selectionchanged event
{
   listBox1.ItemsSource = InitialFeed.Where(itm => itm.SomeSubProperty == listbox1.SelectedValue)
                                     .ToList();

}

上面发生的事情是原始数据被保存并显示没有过滤器。然后,用户从另一个列表框中选择一些内容,这会导致我们过滤掉他们选择的内容,并使用一组新数据重新绑定 lisBox1

请注意,这将起作用,但不是MVVM的方式。我在博客文章Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.

上给出了一个关于MVVM和基本绑定的快速示例