将列表框绑定到对象列表 [英] Binding Listbox to list of object

查看:77
本文介绍了将列表框绑定到对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是代码页:

Below is the code page:

public partial class DataBinding : UserControl<br />    {<br />        public DataBinding()<br />        {<br />            InitializeComponent();<br /><br />            List<PlaylistItem> lstPLI = new List<PlaylistItem>();<br />            for (int i = 0; i < 3; i++)<br />            {<br />                lstPLI.Add(new PlaylistItem("Name" + i.ToString(), "Path" + i.ToString()));<br /><br />            }<br />            lstPlayList.ItemsSource = lstPLI;<br />        }<br />    }<br /><br />    public class PlaylistItem<br />    {<br />        public string FileName;<br />        public string FilePath;<br /><br />        public PlaylistItem()<br />        {<br /><br />        }<br />        public PlaylistItem(string filename,string filepath)<br />        {<br />            FileName = filename;<br />            FilePath = filepath;<br />        }<br />    }




这是xaml页面:




And here is the xaml page:

<UserControl x:Class="FirstSilverLight.DataBinding"<br />    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br />    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" <br />    Width="400" Height="300"><br />    <Grid x:Name="LayoutRoot" Background="White"><br />        <ListBox x:Name="lstPlayList"><br />            <ListBox.ItemTemplate><br />                <DataTemplate><br />                    <StackPanel Orientation="Horizontal"><br />                        <TextBlock Text="{Binding FileName}"></TextBlock><br />                        <TextBlock Text="{Binding FilePath}"></TextBlock><br />                    </StackPanel><br />                </DataTemplate><br />            </ListBox.ItemTemplate><br />        </ListBox><br />    </Grid><br /></UserControl>



列表框未显示任何项目当我运行它时,它虽然在调试时Listbox items.count是3.知道是什么问题吗?



The Listbox doesnot show any items in it when I run this, though Listbox items.count is 3 when i debug this. Any idea what the problem is?

推荐答案

使字符串属性代替字段:
Make the strings properties instead of fields:
public string FileName { get; set; }<br />    public string FilePath { get; set; }






这篇关于将列表框绑定到对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆