Paulo Flores
Paulo Flores

Reputation: 11

What's wrong with my Windows Phone 7 Databinding(no viewmodels used)?

I am having difficulty in databinding. I can successfully get the results but it just won't display. here is my code:

private List<FacebookFriend> friendList;    
public List<FacebookFriend> FriendList
        {
            get { return friendList; }
            set
            {
                friendList = value;
                NotifyPropertyChanged("FriendList");
            }
        }
private void GetFbFriends()
    {
        var fb = new FacebookClient(_accessToken);
        friendList = new List<FacebookFriend>();
        fb.GetCompleted += (o, e) =>
        {
            if (e.Error != null)
            {
                return;
            }
            var result = (JsonObject)e.GetResultData();

            foreach (var friend in (JsonArray)result["data"])
                friendList.Add(new FacebookFriend()
            {
                Id = (string)(((JsonObject)friend)["id"]),
                Name = (string)(((JsonObject)friend)["name"])
            });
            FriendList = friendList;
        };
        fb.GetAsync("me/friends");
    }

then in the page's xaml:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding FriendList}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <StackPanel Background="Red" Height="100" Width="300" Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=Name}"/>
                                <TextBlock Text="{Binding Path=Id}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

It seems correct but still, it does not display anything. Any help is appreciated. Thanks so much!

Upvotes: 0

Views: 121

Answers (1)

Milan Aggarwal
Milan Aggarwal

Reputation: 5104

Try using ObservableCollection<> instead of list<>. For more info please see this

Note: ObservableCollection is a generic dynamic data collection that provides notifications (using an interface "INotifyCollectionChanged") when items get added, removed, or when the whole collection is refreshed.

Upvotes: 1

Related Questions