ListBox not populating data binding in Silverlight 2
So I'm trying to learn Silverlight, so I created a simple demo application that pulls my home feed from FriendFeed and displays the items in a list.
I have a specific list:
<ListBox x:Name="lstItems" Margin="5,61,5,5" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="8,8,43,8">
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
which is populated with a web service call
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
FriendFeedServiceClient client = new FriendFeedServiceClient();
client.GetHomeCompleted += new EventHandler<GetHomeCompletedEventArgs>(client_GetHomeCompleted);
client.GetHomeAsync(FfUsername.Text, FfApiKey.Password);
}
void client_GetHomeCompleted(object sender, GetHomeCompletedEventArgs e)
{
lstItems.DataContext = e.Result;
}
FriendFeedServiceClient
makes a call to the local web service that proxies the request to the actual FriendFeed web service.
The service call works fine, the items are returned, if I debug the call, the property is lstItems.DataContext
filled with a list of items with data in them, but nothing is displayed in the list, it is always empty. Did I miss something?
source to share
You need to link your list, something like this
<ListBox x:Name="lstItems" Margin="5,61,5,5" Grid.Row="1" ItemsSource="{Binding}">
and then binding the TextBlock to the title of the path should work.
EDIT: You are setting the DataContext, which type suggests that you are probably binding to a custom object, have you tried to route e.GetResult to your custom object, something like
YourCustomObject obj = (YourCustomObject) e.GetResult;
lstItems.DataContext = obj;
NTN
source to share
You are not binding to DataContext.
Try adding ItemsSource="{Binding}"
:
<ListBox x:Name="lstItems" Margin="5,61,5,5" Grid.Row="1" ItemsSource="{Binding}">
Then make sure both classes and the Title property of your object are not private. Also check the output (int output window in visual studio) if there are Binding error messages and let us know.
source to share
Instead, DataContext
you must install ItemsSource
. If you are using DataContext
then you must set ItemsSource
with a binding, however this level of indirection is not unnecessary for what you are trying to do.
Please see the MSDN article for more details on enumerating data in a ListBox .
source to share