views:

155

answers:

1

I am trying to define an itemscontrol and data bind it to a List and the code is as below.
XAML

<ItemsControl x:Name="ic" >  
 <ItemsControl.ItemsPanel>  
  <ItemsPanelTemplate>  
   <StackPanel />  
  </ItemsPanelTemplate>  
 </ItemsControl.ItemsPanel>  
 <ItemsControl.ItemTemplate>  
  <DataTemplate>  
   <StackPanel>  
    <TextBlock Text="{Binding val}" TextWrapping="Wrap" Width="195" />  
   </StackPanel>  
  </DataTemplate>  
 </ItemsControl.ItemTemplate>  
</ItemsControl>  

Item Class

public class Item  
{  
    public string val;  
}  

XAML.cs

public MainPage()  
    {
       InitializeComponent();

        List<Item> items = new List<Item>();
        Item item1 = new Item();
        item1.val = "iasl;fdj1";


        items.Add(item1);

        Item item2 = new Item();
        item2.val = "iasfdkasdkljf2";

        items.Add(item2);

        ic.ItemsSource = items;
    }

The items are displayed when I run this. Am I missing something?

A: 

Binding only operates on properties. Change you Item class to:-

public class Item   
{   
    public string val {get; set;}
} 
AnthonyWJones