Silverlight 3.0中itemscontrol的数据绑定

发布于 2024-08-30 09:46:56 字数 1087 浏览 2 评论 0原文

我正在尝试定义一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-09-06 09:46:56

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

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

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

public class Item   
{   
    public string val {get; set;}
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文