views:

21

answers:

2

I've got a UserControl with a ObservableCollection member.

In the UserControl i have a ListBox and i want the ListBox ItemsSource to be the the UserControls ObservableCollection.

UserControl:

public partial class Timeline : UserControl
{
    public ObservableCollection<TwitterStatus> Tweets = new ObservableCollection<TwitterStatus>();

    public string TwitterTimeline { get; set; }
    public string Title { get; set; }

    public Timeline()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(UserControl_Loaded);
    }

    public void RefreshTimeline(TwitterResponse<TwitterStatusCollection> twitterResponse)
    {
        Tweets.Clear();
        foreach (TwitterStatus t in twitterResponse.ResponseObject)
        {
            Tweets.Add(t);
        }
        this.ApplyDataBinding();
    }

    private void ApplyDataBinding()
    {
        TweetList.ItemsSource = null;
        TweetList.ItemsSource = Tweets;
    }


    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        this.ApplyDataBinding();
    }

}

I think I've tried every possible way, i just cant get it to work. The ListBox is just empty. Am i doing something wrong?

A: 

Might be a very inane question - but have you set the DataSource for the UserControl in the ctor or someplace else (e.g. the XAML)

this.DataSource = this; 

Also look at the output window and see if you see any binding errors (i.e. containing System.Data )

Gishu
A: 

My bad... the solution in the question works fine. Human error.

Henriksjodahl