绑定到对象:以上都是绑定的对象的属性,更常见的是绑定到对象。
A more common situation, however, is that you will want to bind the controls to one object at a time from a collection of objects.
Figure 8-18 shows an example. This program is similar to the example in the previous section, but with the addition of a ComboBox that contains the names of four people. When you select a person in the ComboBox, the program shows that person’s information in the area at the top.
设计器中添加三个label分别显示person的三个属性:
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="5,10">
<Label Name="lblFName" FontWeight="Bold"/>
<Label Name="lblAge"/>
<Label Name="lblColor"/>
</StackPanel>
<ComboBox Name="comboPeople" SelectedIndex="0"/>
</StackPanel>
The following is the code-behind. It creates an array of four Person objects and assigns the array to the ItemsSource property. The declaration of the Person class is the same as