views:

922

answers:

3

So I'm trying to learn Silverlight so I've built a simple demo app that pulls my home feed from FriendFeed and displays the items in a list.

I've got a listbox defined:

    <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 being populated by 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;
}

The FriendFeedServiceClient is doing a call to a local webservice that proxies a request to the actual FriendFeed webservice.

The service call works fine, the items are returned, if I debug the call the lstItems.DataContext property is populated with a list of items with data in them, but the list doesn't display anything, it's always blank. Have I missed something?

+1  A: 

Hi,
You need to bind your Listbox, something like this

<ListBox x:Name="lstItems" Margin="5,61,5,5" Grid.Row="1" ItemsSource="{Binding}">

and then the TextBlock's binding to the path Title should work.

EDIT: You are setting the DataContext, which kind of gives a hint that you are probably binding a custom object, have you tried casting the e.GetResult to your custom object, something to the likes of

YourCustomObject obj = (YourCustomObject) e.GetResult;
lstItems.DataContext = obj;

HTH

Anand
Unfortunately that hasn't changed anything. Service still responds with data, then does nothing
Glenn Slaven
Please paste some dummy values that you are receiving in e.GetResult.If your e.Getresult is not sending you and custom business objects then in the code behind try setting the ListBox.ItemsSource instead of DataContext
Anand
I've found the problem, the service was returning objects that was using fields not properties, and while the service would populate these objects in debug, they weren't available to the XAML property getters
Glenn Slaven
Now getting a new problem though: http://stackoverflow.com/questions/820376/silverlight-serialization-of-a-net-web-service-causes-xml-error
Glenn Slaven
+1  A: 

You're 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 that both class and Title property of your object are not private. Also check output (int output window in visual studio) if there are any Binding error messages and let us know.

jarek
+1  A: 

Rather than DataContext you should be setting ItemsSource. If you use DataContext then you have to set ItemsSource with a binding, however, this level of indirection is rather unnecessary for what you're trying to do.

See this MSDN article for details on listing data in the ListBox.

Jeff Yates