views:

86

answers:

2

I found this XAML in an example:

<Window x:Class="TestDataTemplate123.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestDataTemplate123"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:Customer x:Key="customers"/>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="Customers:"/>
        <ListBox ItemsSource="{Binding Source={StaticResource customers}}"/>
    </StackPanel>
</Window>

I'm trying to create the code behind but the following doesn't this work (it displays an empty set (I'm expecting a list of words that say TestDataTemplate123.Customer).

Window1.cs

using System.Collections.ObjectModel;
using System.Windows;

namespace TestDataTemplate123
{
    public partial class Window1 : Window
    {
        public static ObservableCollection<Customer> Customers()
        {
            return Customer.GetAllCustomers();
        }

        public Window1()
        {
            InitializeComponent();
        }
    }
}

Customer.cs:

using System.Collections.ObjectModel;

namespace TestDataTemplate123
{
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        public static ObservableCollection<Customer> GetAllCustomers()
        {
            ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
            customers.Add(new Customer() {FirstName = "Jim", LastName = "Smith", Age = 23});
            customers.Add(new Customer() {FirstName = "John", LastName = "Jones", Age = 22});
            customers.Add(new Customer() {FirstName = "Jay", LastName = "Anders", Age = 21});
            return customers;
        }
    }
}

I've tried many different combinations of xmlns:local, x:Key, StaticResource, DataContext but I get various errors or an empty ListBox.

What do I have to change so that I it just lists out TestDataTemplate123.Customer so I can go on making my DataTemplate?

+2  A: 

Your XAML has you binding the items in your list to a single instance of Customer. You need to bind to a list of customers.

Since you're doing most of the work in the code-behind, I'd ditch the <local:Customer /> declaration in the XAML, as well as the ItemsSource attribute, and set it in your code:

public Window1()
{
    InitializeComponent();
    listBox1.ItemsSource = Customer.GetAllCustomers();
}

Note that I've given the ListBox a name so I can refer to it from the code behind. Do that like this:

<ListBox x:Name="listBox1" />
Matt Hamilton
right, I could do that to get it to work, but given the XAML above, what is the code behind they are using? I'm trying to (1) learn this new syntax and (2) in general do less with code-behind and more with XAML, I actually got this from http://msdn.microsoft.com/en-us/library/ms742521.aspx where they have <local:Tasks x:Key="myTodoList"/> and <ListBox ItemsSource="{Binding Source={StaticResource myTodoList}}"/>, so if they are binding the whole class "Tasks" I don't see where they are getting the collection of objects from.
Edward Tanguay
Gishu's got the right answer if you want to make it work purely given the code you have. ObjectDataProvider lets you instantiate an instance of a type and call a method to retrieve values. Be aware that (AFAIK) Silverlight has no ObjectDataProvider, so that might affect your choice whether to use it.
Matt Hamilton
Thanks for the tip, right, programming in WPF is like stepping through a mine field if you eventually want to port your code to Silverlight, you have to know where there mines are hidden.
Edward Tanguay
+2  A: 

You need to create a DataSourceProvider derivation ObjectDataprovider that returns the list of objects to display

<Window.Resources>
  <ObjectDataProvider x:Key="Customers" 
       ObjectType="{x:Type local:Customer}" 
       MethodName="GetAllCustomers" />
</Window.Resources>
...
<ListBox DataContext="{StaticResouce Customers}" ItemsSource="{Binding}" ... />
Gishu