2010-04-29 57 views
1

我想要定义一个项目控件并将数据绑定到列表,代码如下。
XAMLSilverlight 3.0中项目控件的数据绑定

<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> 

项目类

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; 
    } 

当我运行这个时显示的项目。我错过了什么吗?

回答

1

绑定仅对属性进行操作。将您的物品类更改为: -

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