如何将数据动态绑定到ListBox控件? [英] How to bind data to ListBox Control dynamically?

查看:212
本文介绍了如何将数据动态绑定到ListBox控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的窗口Phone 7应用程序。我是新来的Silverlight的。我试图绑定字符串列表框的列表。我使用下面的code

I am developing window phone 7 application. I am new to the silverlight. I am trying to bind a List of strings to Listbox. I am using the following code

ObservableCollection<String> abc = new ObservableCollection<String>();
            abc.Add("XYZ");
            IncomeSummaryListBox.ItemsSource = abc;

我的XAML包含下列code

My xaml contains the following code

<ListBox Margin="16,217,6,275" Name="IncomeSummaryListBox">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">

                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

通过上面的code我的列表框显示在运行时没有任何项目。我的模拟器设备没有显示在列表框中任何东西。什么是错误的,我code?能否请您给我提供任何code或链接上面的问题,这样我就可以解决上述问题?如果我做错什么,那么请指导我。

With the above code my listbox shows no items at runtime. My emulator device doesn't show anything in listbox. What is wrong in my code ? Can you please provide me any code or link for the above issue so that I can resolve the above issue ? If I am doing anything wrong then please guide me.

推荐答案

您还没有告诉实际数据模板来显示项目。在显示的字符串,如果你只是删除整个的这个简单的例子 ListBox.ItemTemplate 从你的XAML将开始工作。

You've not actually told the data template to display the item. In this simple case of displaying strings if you just delete the whole ListBox.ItemTemplate from your xaml it will start working.

有关学术原因,你可以使用: -

For academic reasons you could use:-

<ListBox Margin="16,217,6,275" Name="IncomeSummaryListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

这结合的的TextBlock 文本属性从的ItemsSource的项目。

This binds the Text property of a TextBlock to an item from the ItemsSource.

这篇关于如何将数据动态绑定到ListBox控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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