2012-07-22 71 views
0

我试图编写一个rssreader,并且会对某些架构提示感到满意。 我的阅读器主窗口包含两个加载到框架中的wpf页面,这是一个“底部栏”,用户可以选择不同的rss提供程序。在主框架(或页面)是我的列表视图。 由于加载动画和UI冻结我有一个额外的类与backgroundworker填充RSS数据的可观察集合,当我调试时,它正确填充我的集合。 在主页我设置datacontext这个可观察的收藏,但列表视图不显示任何东西,在这里我卡住了。Binding ObservableCollection of objects trouble

这就是我:

XAML的MainPage:

> <ListBox ItemsSource="{Binding}" DisplayMemberPath="RssTitle" 
> IsSynchronizedWithCurrentItem="True" 
> SelectionChanged="itemsList_SelectionChanged" 
> ItemContainerStyle="{DynamicResource listboxitem_style}" Height="396" 
> HorizontalAlignment="Left" Margin="126,12,0,0" Name="ListBox1" 
> VerticalAlignment="Top" Width="710"></ListBox> 

ListBox1.DataContext = GetRssItems.observable_list; 

Bottompage得到另一个RSS供稿:

GetRssItems getitems = new GetRssItems(); 
GetRssItems.observable_collection = null; 
getitems.start_bg_worker("url"); 

GetRssItems.cs

public class GetRssItems 
    { 
     public static ObservableCollection<RSSItem> observable_collection { get; set; } 
     public static string tmp_url; 
     public BackgroundWorker worker = new BackgroundWorker(); 


     public void start_bg_worker(string url) 
     { 


      if (!worker.IsBusy) 
      { 

       worker.DoWork += new DoWorkEventHandler(worker_DoWork); 
       worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); 
       worker.RunWorkerAsync(url); 
      } 
     } 
} 

在BackgroundWorkers DoWork的我收到的LINQ RSS项目,并将其添加到我的观察到的集合:

observable_collection.Add(new RSSItem(item.tmp_Title, item.tmp_Link, item.tmp_Description, item.tmp_pubDate, item.tmp_ImageUrl)); 

独立的类RSSItem.cs

public class RSSItem 
    { 
     public string RssTitle { get; set; } 
      public string RssLink { get; set; } 
      public string RssDescription { get; set; } 
      public string RsspubDate { get; set; } 
      public string RssImageUrl { get; set; } 

      public RSSItem(string rsstitle, string rsslink, string rssdescription, string rsspubdate, string rssimageurl) 
      { 
       RssTitle = rsstitle; 
       RssLink = rsslink; 
       RssDescription = rssdescription; 
       RsspubDate = rsspubdate; 
       RssImageUrl = rssimageurl; 
      } 
    } 

感谢您的时间和提示。 问候

回答

1

使用以下命令: 数据上下文应该是对象getitems

<ListBox ItemsSource="{Binding observable_collection}" Height="167" Margin="0" Name="listBox1" Width="330" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Top"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding RssTitle}" FontWeight="Bold" FontSize="16" /> 
           <TextBlock Text="{Binding RssLink}" FontSize="16"/> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 

PS: 您的命名是HORRBILE

+0

哦,你说得对,命名实际上是非常可怕的。试图改变我的列表框,当你回答,但它不起作用。 Bindings属性中的observable_collection是否已知? – nukleos 2012-07-22 16:14:10

+0

许多感谢Nahum,它现在正在工作:) – nukleos 2012-07-22 22:07:33

+0

@nukleos即使是一次性使用5行代码,也永远不会写出错误命名的代码。 再次阅读有关MVVM模式后,我建议您结帐 MVVMLight或Prism框架。将为您节省大量麻烦。 – Nahum 2012-07-23 06:12:33

2

您需要阅读了一下MVVM得到最受益于WPF。你的行设置列表框的datacontext相当混乱。

你应该有什么是你的主窗口的(xaml)数据上下文设置为包含您的可观察集合的视图模型类。列表框的ItemsSource被设置为该属性名称。

例如:

public class MainViewModel : INotifyPropertyChanged 
{ 
    public ObservableCollection<RSSItem> RSSItems 
    { 
     get; 
     set; 
    } 
    // Other stuff applicable to the main window. 
} 

当视图被构造,通过MainViewModel的实例,以它的DataContext的。然后,XAML中的列表框将是:

<ListBox ItemsSource={Binding Path=RSSItems} ... /> 

如果你想能够设置/更改的RSSItems收集实例(即公共setter方法),那么你应该用NotifyPropertyChanged事件设置它,它的制定者,但如果你只需添加/删除项目,那么这不应该是必要的。 (也就是加载填充构造函数中的项目)。

+0

感谢您的提示,目前阅读了一些关于MVVM的文章。 – nukleos 2012-07-22 22:09:35

相关问题