So far, you've seen some examples of binding controls to a single object. However, the more complex use is bound to a list of objects. For example, imagine that our object data source can create a new type that represents a list of person objects, just as example 4-19:
Example 4-19
using System.Collections.Generic; // List<T>
namespace PersonBinding {
// XAML doesn't (yet) have a syntax
// for generic class instantiation
class People : List<Person> {}
}
We can hang up this new list of data sources and bind to it in the same way as if it were bound to a separate object data source, like example 4-20.
Example 4-20
<!-- Window1.xaml -->
<?Mapping XmlNamespace="local" ClrNamespace="PersonBinding" ? >
<Window xmlns:local="local">
<Window.Resources>
<local:People x:Key="Family">
<local:Person Name="Tom" Age="9" />
<local:Person Name="John" Age="11" />
<local:Person Name="Melissa" Age="36" />
</local:People>
<local:AgeToForegroundConverter
x:Key="AgeToForegroundConverter" />
</Window.Resources>
<Grid DataContext="{StaticResource Family}">
<TextBlock >Name:</TextBlock>
<TextBox Text="{Binding Path=Name}" />
<TextBox
Text="{Binding Path=Age}"
Foreground="{Binding Path=Age, Converter=}" />
<Button >Birthday</Button>
</Grid>
</Window>
In Example 4-20, we created an example of a people collection and imported it through three person objects. However, running it will be as shown in Figure 4-6.