I’m unable to do a simple but yet tricky WPF binding in Silverlight 4 (WP7 development)
I have the following code:
Class People{
public string firstname;
public string lastname;
}
Class DataSource{
public static List<People> people; // consider this as a list filled with objects already
}
I’m trying to put the list of people into a ListBox, here’s the xaml I’ve tried:
<ListBox x:Name="peoplelistbox" Margin="0,0,-12,0" ItemsSource="{Binding DataSource.people}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding firstname}" TextWrapping="Wrap"/>
<TextBlock Text="{Binding lastname}" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But unfortunately my listbox remains empty. What am I doing wrong ?
Thank you in advance 🙂
Cheers,
Miloud B.
Firstly you are using fields, where you should use
publicproperty (i.e. people, firstname and lastname). Convertpeopleto a public property, like this:Then, you need to bind the
ItemsSourceusingx:Staticmarkup, like this:PS:
localis the xml namespace pointing to yourDataSourceclass’s namespace. Also, your class too needs to be a public class.EDIT:
For WP7, you need to declare the instance of the class in the resources and then you can use
Pathto point to the source. Like this:PS: Again, your class needs to be public and must have a default constructor.
EDIT:
Here is a example which is working perfectly on my system. Check and see where are you making the mistake:
Xaml (only the relevant portions):