views:

254

answers:

2

I have a listbox in my silverlight usercontrol and I am filling it with a generic list of a private class, for some reason it is not being databound.

Here is the code :

class userClient
{
    public int characterID { get; set; }
    public string characterName { get; set; }
}

List<userClient> userClientList; // = new List<userClient>();

void _client_UserList(object sender, DataTransferEventArgs e)
{
    this.Dispatcher.BeginInvoke(() =>
    {
        userClientList = new List<userClient>();
        foreach (string user in e.DataTransfer.Data)
        {
            var userDetailsArray = user.Split('+');
            userClient uc = new userClient
            {
                characterID = Convert.ToInt32(userDetailsArray[0]),
                characterName = userDetailsArray[1]
            };             
            userClientList.Add(uc);
        }

        chatUsers.ItemsSource = userClientList;
        chatUsers.DisplayMemberPath = "characterName";
    });
}

I checked the generic list userClientList and it is being filled up so there is no problems there.

This is the XAML of the listbox:

<ListBox x:Name="chatUsers" Grid.Row="0" Grid.Column="1" Margin="2 2 2 2" />
+3  A: 

Do you have any binding errors messages logged in Output Window in Visual Studio?

Edit:

Just noticed that your collection is a field while it should be public property

public List<userClient> userClientList { get; set; }
jarek
Forgot about the output window the following error popped up:Cannot get 'characterName' value (type 'System.String') from 'Yambushi.CombatRoom+userClient' (type 'Yambushi.CombatRoom+userClient'). BindingExpression: Path='characterName' DataItem='Yambushi.CombatRoom+userClient' (HashCode=56478042); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: userClient.get_characterName()
Drahcir
This means that binding engine do not have access to the getter of a property. make your userClientList collection public, userClient class public and properties in it public as well.
jarek
A: 

Thank you! Thank you Mokosh! The error -- System.Windows.Data Error: 39 : BindingExpression path error -- drove me crazy through two WPF books without finding the answer that you emphasized -- I didn't create properties on my class members.