views:

38

answers:

1

My code behind file receives an instance object Session which has a property AvailableCountries that returns a List. Each Country object has a Name property which is a String.

I also want to display these Country objects using a data template which I simplified here.

My current code is after going through a WPF binding tutorial, only to discover that you couldn't bind to instance objects using XAML, so I modified it as follow based on another tutorial, but it still displays nothing.

I have another method that fills a second listbox manually which tells me that the country list is indeed passed correctly.

<UserControl.Resources>
    <DataTemplate x:Key="countriesLayout" DataType="Country">
        <StackPanel TextBlock.Foreground="Yellow">
            <StackPanel HorizontalAlignment="Left">
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

<ListBox    Name="ctrlCountries"
            ItemTemplate="{DynamicResource countriesLayout}"
            IsSynchronizedWithCurrentItem="True"
/>

// In my code behind file I have:
private void onLoad(object sender, RoutedEventArgs e) {
    ctrlCountries.DataContext = Session.AvailableCountries;
}
+1  A: 

You need to set the ItemsSource property of your listbox to binding:

<ListBox    Name="ctrlCountries" 
            ItemTemplate="{DynamicResource countriesLayout}" 
            IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding}"
/> 
mattythomas2000
Thank you!(for any other readers: put quotes around {Binding})
PRINCESS FLUFF
Sorry about that, put the answer in too quickly. Thanks for adding the comment about the error
mattythomas2000