0

我在数据绑定方面遇到了困难。我可以成功获得结果,但它不会显示。这是我的代码:

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");
    }

然后在页面的 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>

这似乎是正确的,但仍然没有显示任何内容。任何帮助表示赞赏。非常感谢!

4

1 回答 1

1

尝试使用ObservableCollection<>而不是list<>. 有关更多信息,请参阅

注意:ObservableCollection是一个通用的动态数据集合,当items get added, removed, or when the whole collection is refreshed.

于 2012-11-28T07:57:06.683 回答