问题描述
比方说,比如我有以下非常简单的窗口:
Lets say for instance i have the following extremely simple window:
<Window x:Class="CalendarGenerator.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="447">
<Grid>
<ListBox Margin="12,40,0,12" Name="eventList" HorizontalAlignment="Left" Width="134" />
</Grid>
</Window>
和定义为一个简单的列表:
And a simple list defined as:
List<String> ListOfNames = new List<String>();
和让我们假设该列表中有好几个名字。我将如何去使用尽可能多的$ C $列表绑定到ListBox C-后面越好?
And lets assume that the list has several names in it. How would i go about binding the List to the ListBox using as much code-behind as possible?
推荐答案
首先你需要给你的列表框的名称,以便它是从后面(的修改的我注意到您的code访问你已经做到了这一点,所以我会改变我的例子ListBox的名称来反映你的):
First you'd need to give your ListBox a name so that it's accessible from your code behind (edit I note you've already done this, so I'll change my example ListBox's name to reflect yours):
<ListBox x:Name="eventList" ... />
然后,它与设定ListBox的ItemsSource属性列表:
eventList.ItemsSource = ListOfNames;
既然已经定义了你ListOfNames对象作为列表与LT;弦乐&GT;
,列表框不会自动反映到列表中所做的更改。为了让WPF的数据绑定反应到列表中的更改,将其定义为ObservableCollection<$c$c><String>$c$c>来代替。
Since you've defined your "ListOfNames" object as a List<String>
, the ListBox won't automatically reflect changes made to the list. To get WPF's databinding to react to changes within the list, define it as an ObservableCollection<String>
instead.
这篇关于编程结合清单列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!