Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Below is my code that returns RelationList type IEnumeration.

C#
public IEnumerable<RelationList> GetRelations(string userID)
  {
      return from relations in dbContext.RelationList
                   where relations.PrimaryUserID == userID
                   select relations;
  }


After this I use below code to bind the Enumeration with ListBox:

C#
listRelations.ItemsSource = GetRelations(userID);


It binds and shows proper number of rows, but values that shows up is "RelationList".

Actual data is in RelationList.UserName

How can I get it to show up in ListBox?
Posted
Comments
Tarun.K.S 17-Feb-11 8:49am    
What is RelationList?
Wild-Programmer 17-Feb-11 9:03am    
RelationList is Entity model (from table view). It has columns like UserName, UserType, Status.

Provide a DataTemplate for the ListBox :

XML
<ListBox.ItemTemplate>
    <DataTemplate>
         <TextBlock Text="{Binding UserName}"/>
    </DataTemplate>
</ListBox.ItemTemplate>
 
Share this answer
 
Comments
Wild-Programmer 17-Feb-11 9:04am    
I tried this, doesnt work :(
Nish Nishant 17-Feb-11 9:05am    
That's probably because you don't have an UserName property. Use a property that actually exists on your object :-)
Wild-Programmer 17-Feb-11 9:40am    
If I use his answer, I get error - "Items collection must be empty before using ItemsSource."
Think before jumping to conclusion.
Nish Nishant 17-Feb-11 9:44am    
Are you setting items elsewhere either in code or in Xaml?
Wild-Programmer 17-Feb-11 10:05am    
No :(
I just wrote my code in code behind and then Nishant's code. Thats it.
Ok, this is in response to your response (which some mod will move to a comment, or to your original question). You now have:

XML
<ListBox Height="367" HorizontalAlignment="Left" Name="listRelations"
       VerticalAlignment="Top" Width="254">
    <DataTemplate>
        <TextBlock Name="txtName" Text="Binding UserName" />
    </DataTemplate>
</ListBox>


Can you see what's wrong there? You are inadvertently setting the Items property by adding a DataTemplate as an item in your ListBox's Items collection. The correct code is:

XML
<ListBox Height="367" HorizontalAlignment="Left" Name="listRelations"
       VerticalAlignment="Top" Width="254">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <TextBlock Name="txtName" Text="{Binding UserName}" />
      </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
 
Share this answer
 
v3
Comments
Wild-Programmer 17-Feb-11 10:22am    
Bang my head :(Still not working. Error Message disappeared.
Now it shows 3 rows in Listbox with text "Binding UserName" in all of them.
Can I email you my code. Its a small one.
Nish Nishant 17-Feb-11 10:24am    
What error do you get now?
Wild-Programmer 17-Feb-11 10:25am    
3 rows in Listbox with text "Binding UserName" in all of them.
Nish Nishant 17-Feb-11 10:26am    
Missing braces, see my updated answer :-)
Wild-Programmer 17-Feb-11 10:31am    
I am sorry to disappoint you but it is still not showing :(
No Error, blank 3 rows. Debug mode shows values.
Please take my code and see what is the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900