Silverlight 3.0中itemscontrol的数据绑定
我正在尝试定义一个 itemscontrol 并将其数据绑定到一个列表,代码如下。
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>
项目类
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;
}
当我运行此项目时,将显示这些项目。我错过了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定仅对属性进行操作。将您的项目类别更改为:-
Binding only operates on properties. Change you Item class to:-