C# 以编程方式将 List 绑定到 ListBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/449410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically binding List to ListBox
提问by Jason Miesionczek
Let's 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>();
And let's 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?
让我们假设列表中有几个名字。我将如何使用尽可能多的代码隐藏将 List 绑定到 ListBox?
采纳答案by Matt Hamilton
First you'd need to give your ListBox a name so that it's accessible from your code behind (editI note you've already done this, so I'll change my example ListBox's name to reflect yours):
首先,您需要为您的 ListBox 命名,以便可以从您的代码后面访问它(编辑我注意到您已经这样做了,所以我将更改我的示例 ListBox 的名称以反映您的名称):
<ListBox x:Name="eventList" ... />
Then it's as simple as setting the ListBox's ItemsSourceproperty to your list:
然后就像将 ListBox 的ItemsSource属性设置为您的列表一样简单:
eventList.ItemsSource = ListOfNames;
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.
由于您已将“ListOfNames”对象定义为 a List<String>
,因此 ListBox 不会自动反映对列表所做的更改。要使 WPF 的数据绑定对列表中的更改做出反应,请将其定义为ObservableCollection<String>
。
回答by NotDan
eventList.ItemsSource = ListOfNames;
eventList.ItemsSource = ListOfNames;
回答by aku
Use Bindingclass if you want to customize binding:
如果要自定义绑定,请使用Binding类:
List<String> listOfNames = new List<String>() {"a", "b"};
Binding myBinding = new Binding();
//set binding parameters if necessary
myBinding.Source = listOfNames;
eventList.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
or
或者
directly assign data to ItemsSource property:
直接将数据分配给 ItemsSource 属性:
eventList.ItemsSource = listOfNames;
回答by Cameron MacFarland
If the data list is created in code then you're going to have to bind it in code, like so:
如果数据列表是在代码中创建的,那么您将不得不在代码中绑定它,如下所示:
eventList.ItemsSource = ListOfNames;
Now binding to a list of strings is a very simple example. Let's take a more complex one.
现在绑定到一个字符串列表是一个非常简单的例子。让我们来看一个更复杂的。
Say you have a person class:
假设你有一个 person 类:
public class Person {
public string FirstName { get; set; }
public string Surname { get; set; }
}
To display a list of persons you could bind a list to the ListBox, but you'll end up with a listbox that displays "Person" for each entry, because you haven't told WPF how to display a person object.
要显示人员列表,您可以将列表绑定到 ListBox,但最终会得到一个显示每个条目的“人员”的列表框,因为您还没有告诉 WPF 如何显示人员对象。
To tell WPF how to visually display data objects we define a DataTemplate like so:
为了告诉 WPF 如何直观地显示数据对象,我们定义了一个 DataTemplate,如下所示:
<Window.Resources>
<DataTemplate DataType="{x:Type l:Person}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Surname}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Name="listBox" />
</Grid>
public Window1() {
InitializeComponent();
List<Person> people = new List<Person>();
people.Add(new Person() { FirstName = "Cameron", Surname = "MacFarland" });
people.Add(new Person() { FirstName = "Bea", Surname = "Stollnitz" });
people.Add(new Person() { FirstName = "Jason", Surname = "Miesionczek" });
listBox.ItemsSource = people;
}
This will nicely display "Firstname Surname" in the list.
这将在列表中很好地显示“Firstname Surname”。
If you wanted to change the look to be say "Surname, Firstname" all you need to do is change the XAML to:
如果您想将外观更改为“姓氏,名字”,您需要做的就是将 XAML 更改为:
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" Text="{Binding Surname}"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>