Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Suppose I have these codes:
XML
class Friend
{
    public string Name;
    public Icon icon;
}

List<Friend> myFirends=....;


Now i want to show all members of "myFirends" in this pattern in WPF:

XML
<TextBlock Text="{Binding path=Name}">
<Image Source="{Binding path=icon}">


How is it possible?
Posted

It totally depends on what you want to have as a container for those instances of TextBlock and Image and how you want to present these FrameworkElements. You need to use the container class of the type inheriting from the System.Windows.Controls.ItemsControl, see http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx[^]. In this case, you can all/remove/bind arbitrary number of arbitrary objects to such container.

See also http://msdn.microsoft.com/en-us/library/bb613548.aspx[^].

Learn about binding:
http://msdn.microsoft.com/en-us/library/ms752347.aspx[^],
http://msdn.microsoft.com/en-us/magazine/cc163299.aspx[^];
Example of binding of ItemsControl: http://www.galasoft.ch/mydotnet/articles/article-2007041201.aspx[^].

—SA
 
Share this answer
 
It is possible but you need a container like the solution above said. You can create a list box and set the Template like this:


XML
<ListBox x:name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>			
<TextBlocktext="{Binding path=Name}">
<Image Source="{Binding path=icon}">
</TextBlock>
</StackPanel>
</DataTemplate>			
</ListBox.ItemTemplate>
</ListBox>


In the code behind you can set the myListBox.ItemsSource = myFriends

 
Share this answer
 
v5
As said you need a container. How you have the object render can be on the containers item template or you can set the objects rendering tempalte elsewhere.

For example:

XAML
<datatemplate datatype="{x:Type">
  <stackpanel>
   <textblock text="{Binding" path="Name}/">
   <image source="{Binding" path="Icon}/">
  </image></textblock></stackpanel>
</datatemplate>


As long as this was pulled in from a resource dictionary or defined in that controls resources it will render the object. Another option is to built the "Friend" view as a user control. That way you can modify it using the designer. If you do that you would do something like this:

XAML
<datatemplate>
   <local:friendview xmlns:local="#unknown" />
</datatemplate>


Where FriendView is a usercontrol set up as you want.

Then just set up your container (as others have pointed out)

XAML
 <listbox itemsource="{Binding" path="MyFriends}/">
</listbox>


Make sure MyFriends is public (you have it as private)
 
Share this answer
 

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