i started a WPF application (with vs 2008 sp1) which connects to a web service to get Collection of objects.
I can be contactInfo[] or groupInfo[].
here is my main.xaml.cs
public main()
{
InitializeComponent();
//service.addContactCompleted +=new addContactCompletedEventHandler(addContactCompleted);
service.getContactsCompleted += new getContactsCompletedEventHandler(getContactsCompleted);
fillContents();
}
private void getContactsCompleted(object sender, getContactsCompletedEventArgs e)
{
try
{
//e.Result return contactInfo[]
contactListBox.ItemsSource = e.Result;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void fillContents()
{
service.getContactsAsync(session.key, null);
}
and this is my main.xaml
<Window.Resources>
<ObjectDataProvider x:Key="contactInfo" ObjectType="{x:Type serviceAdmin:contactInfo}" />
</Window.Resources>
<Grid>
<ListBox Margin="-146,-124,-143,-118.808" Name="contactListBox" ItemsSource="{Binding Source={StaticResource contactInfo}}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding fullName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--<toolkit:DataGrid Margin="-146,-124,-150,-118.808" Name="contactGrid" ItemsSource="{Binding}"/>-->
</Grid>
this partially works but just that it returns repeated values. it just repeats which ever comes first.I’ll like to know what i’m doing wrong here.Can anyone shed some light?? thanks for reading this!!
Looks like you are binding your ListBox to the wrong source. First of all, I don’t really see why you need to have to ObjectDataSource at all. You can just bind the ItemsSource of the ListBox to your collection, like you did. But also (as a commenter pointed out), keep in mind that you are accessing the UI on a different thread, so you should use calls to Dispatcher to fill up your listbox.
Maybe something like this:
your xaml can be greatly simplified to this:
HTH,
Roel