C# 以编程方式将列表绑定到ListBox

C# 以编程方式将列表绑定到ListBox,c#,wpf,data-binding,C#,Wpf,Data Binding,比如说,我有一个非常简单的窗口: <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"

比如说,我有一个非常简单的窗口:

<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>

以及一个简单的列表,定义如下:

List<String> ListOfNames = new List<String>();
List ListOfNames=new List();

让我们假设列表中有几个名字。如何使用尽可能多的隐藏代码将列表绑定到列表框?

首先,您需要为列表框指定一个名称,以便可以从隐藏代码访问列表框(编辑我注意到您已经这样做了,因此我将更改示例列表框的名称以反映您的名称):


由于已将“ListOfNames”对象定义为
列表
,因此ListBox不会自动反映对列表所做的更改。要使WPF的数据绑定对列表中的更改做出反应,请将其定义为一个

eventList.ItemsSource=ListOfNames

如果要自定义绑定,请使用类:

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);

如果数据列表是在代码中创建的,则必须在代码中绑定它,如下所示:

eventList.ItemsSource = ListOfNames;
现在绑定到字符串列表是一个非常简单的示例。让我们再复杂一点。

假设您有一个人类:

public class Person {
    public string FirstName { get; set; }
    public string Surname { get; set; }
}
要显示人员列表,可以将列表绑定到ListBox,但最终会得到一个为每个条目显示“人员”的ListBox,因为您没有告诉WPF如何显示人员对象

为了告诉WPF如何直观地显示数据对象,我们定义了如下数据模板:

<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;
}

公共窗口1(){
初始化组件();
列表人员=新列表();
添加(newperson(){FirstName=“Cameron”,姓氏=“MacFarland”});
添加(newperson(){FirstName=“Bea”,姓氏=“Stollnitz”});
添加(newperson(){FirstName=“Jason”,姓氏=“Miesionczek”});
listBox.ItemsSource=人;
}
这将在列表中很好地显示“Firstname姓氏”

如果要将外观更改为“姓氏,Firstname”,只需将XAML更改为:

<StackPanel Orientation="Horizontal">
    <TextBlock FontWeight="Bold" Text="{Binding Surname}"/>
    <TextBlock Text=", "/>
    <TextBlock Text="{Binding FirstName}"/>
</StackPanel>

public class Person {
    public string FirstName { get; set; }
    public string Surname { get; set; }
}
<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;
}
<StackPanel Orientation="Horizontal">
    <TextBlock FontWeight="Bold" Text="{Binding Surname}"/>
    <TextBlock Text=", "/>
    <TextBlock Text="{Binding FirstName}"/>
</StackPanel>