views:

75

answers:

1

Hello guys! 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!!

+1  A: 

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:

 public main()
{
    InitializeComponent();
    service.getContactsCompleted += new getContactsCompletedEventHandler(getContactsCompleted);
    fillContents();
}

private void getContactsCompleted(object sender, getContactsCompletedEventArgs e)
{
    try
    {
        //e.Result return contactInfo[]
        Dispatcher.Invoke(new Action<List<contactInfo>>(list => contactListBox.ItemsSource = list), e.Result);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

public void fillContents()
{
    service.getContactsAsync(session.key, null);
}

your xaml can be greatly simplified to this:

<Grid>
   <ListBox Margin="-146,-124,-143,-118.808" Name="contactListBox" DisplayMember="fullName" />
</Grid>

HTH,
Roel

Roel
wow! awesome,you are right about the ObjectDatasource, and i removed it. now i want to present the data so ` <!- .....--><ListBox.ItemTemplate><DataTemplate><TextBlock><Label Content="{Binding fullName}" FontSize="15" FontWeight="Bold"/> <LineBreak/><Label Content="{Binding mobile}" FontSize="10" FontStyle="Italic" Foreground="DimGray" /> <Label Content="{Binding email}" FontStyle="Italic" FontSize="10" Foreground="DimGray"/></TextBlock> </DataTemplate></ListBox.ItemTemplate><!- ... -->`will what you are suggesting still work? thanks bruv
black sensei
By the way will the Dispatcher help me calling another web service method asynchronously , it seems that second one is not working property, maybe due to the fact the share the single asmx connection, is there a way around it? thanks for helping!
black sensei
Glad to help. The `DisplayMember` will only work if you want to display just one property. Indeed, you'll have to use an `ItemTemplate` to display different properties.
Roel
No, the Dispatcher is just there to execute calls on your UI thread.
Roel
ok man thanks for this post
black sensei