Binding to Indexed Properties

In addition to binding to nested properties, it's also possible to bind to indexed properties, with both integer and string indexes. The index selection syntax is much the same as it is in C#—simply append the index to the name of the property returning the collection surrounded by square brackets. For example, if a Person object had an Addresses property that returned a collection of Address objects, you could bind to the first Address object using the integer index syntax:

<TextBox DataContext="{StaticResource personResource}"
         Text="{Binding Address[0].AddressLine1}" />

If the collection has string indexes, such as a Dictionary<string, string>, you can refer to the item using the string index syntax, like so:

<TextBox DataContext="{StaticResource personResource}"
         Text="{Binding Address[HOME].AddressLine1}" />

In the preceding example, HOME is the string index of the Address object to return. If the given index is not found, the binding will silently fail, although an error will be written to the Output window in Visual Studio (discussed in the “Debugging Data Binding Issues” section later in this chapter).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.140.153