Listbox not displaying list data at run time wpf

时间:2019-04-16 22:09:57

标签: c# wpf listbox

I’m trying to get tweets to display in a listbox which I have created on the main window of my wpf . The programme compiles but no tweets are displayed and I’m not sure what I’m doing wrong. The code is:

namespace test

{

public partial class MainWindow : Window
{
public MainWindow()

{
InitalizeComponent();
}
static void Program (string[] args)
{
Using (StreamReader r = new StreamReader (“@1234_AllTweets.json”))

{
string json = r.ReadToEnd();
List<Tweet> tweets = JsonConvert.DeserializeObject<List<Tweet>>(json, new IsoDateTimeConverter { DateTimeFormat =“ddd MMM dd:HH:mm:as zz00 yyyy”});

foreach (Tweet tweet in tweets)
{
((MainWindow)Application.Current.MaknWindow).twittermessage.Items.Add(tweet.text);

}
}

1 个答案:

答案 0 :(得分:-1)

Try this:

  1. Instead of using a list of Tweet, use an ObservableCollection of Tweet
  2. Add each tweet to the OC created above
  3. In you XAML, use Binding to link your listbox content to the tweets ObservableCollection

Code for the XAML binding would be something like

<ListBox ItemsSource="{Binding Path=Tweets}" />