"Programming WPF" Translation 4th Chapter 3. Bind to Data list

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.