Indexer Support
In R2 2016, Telerik introduced Indexer Support for RadPropertyGrid's PropertyDefinition bindings. It covers the following scenarios:
For the purposes of this article, we will assume the following class hierarchy is present:
Example 1: The class hierarchy
public class League
{
public string Name { get; set; }
public ObservableCollection<Club> Clubs { get; set; }
public Dictionary<string, Club> ClubsDictionary { get; set; }
}
public class Club
{
public string Name { get; set; }
public DateTime Established { get; set; }
public int StadiumCapacity { get; set; }
public ObservableCollection<Player> Players { get; set; }
public Player this[int index]
{
get { return this.Players.First(x => x.Number == index); }
}
}
public class Player
{
public string Name { get; set; }
public int Number { get; set; }
public string Position { get; set; }
}Binding to an Item of a Collection Through Indexer
A common scenario when using RadPropertyGrid is when we have a collection member of our business object. Example 2 shows how to bind to an item at a specified index in the collection:
Example 2: Binding to a collection
<telerik:RadPropertyGrid Item="{Binding League}" AutoGeneratePropertyDefinitions="False">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition Binding="{Binding Clubs[0]}" DisplayName="Name" />
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>
Binding of NestedProperties
You can also use indexing in NestedProperties, as shown in Example 3:
Example 3: Binding of NestedProperties
<telerik:RadPropertyGrid Item="{Binding League}" AutoGeneratePropertyDefinitions="False" NestedPropertiesVisibility="Visible">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition Binding="{Binding Clubs[0]}" DisplayName="Club">
<telerik:PropertyDefinition.NestedProperties>
<telerik:PropertyDefinition DisplayName="Goalkeeper Name" Binding="{Binding Players[0].Name}"/>
</telerik:PropertyDefinition.NestedProperties>
</telerik:PropertyDefinition>
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>
Binding to a Collection Property with a String Key
As our League class has a Dictionary of clubs, we can access any club by its name, as demonstrated in Example 4.
Example 4: Binding to item in Dictionary
<telerik:RadPropertyGrid Item="{Binding League}" AutoGeneratePropertyDefinitions="False" NestedPropertiesVisibility="Visible">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition Binding="{Binding ClubsDictionary[Liverpool]}" DisplayName="Club">
<telerik:PropertyDefinition.NestedProperties>
<telerik:PropertyDefinition DisplayName="Goalkeeper Name" Binding="{Binding Players[0].Name}"/>
</telerik:PropertyDefinition.NestedProperties>
</telerik:PropertyDefinition>
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>
Binding to an Item which Implements an Indexer
Since our Club class implements an indexer, we can also bind to the Player, whose number is 25, for example:
Example 5: Indexing a class that implements indexer
<telerik:RadPropertyGrid Item="{Binding League}" AutoGeneratePropertyDefinitions="False">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition Binding="{Binding ClubsDictionary[Liverpool][25]}" DisplayName="Player with number 25" />
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>
Using Unbound Mode
Indexer support also works when AutoGenerateBindingPaths is set to False.
Example 6: Indexing when AutoGenerateBindingPaths is set to False
<telerik:RadPropertyGrid Item="{Binding League}" AutoGenerateBindingPaths="False" AutoGeneratePropertyDefinitions="False" NestedPropertiesVisibility="Visible">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition Binding="{Binding ClubsDictionary[Liverpool]}" DisplayName="Club">
<telerik:PropertyDefinition.NestedProperties>
<telerik:PropertyDefinition DisplayName="Chelsea Goalkeeper Name" Binding="{Binding Clubs[Chelsea].Players[0].Name}"/>
</telerik:PropertyDefinition.NestedProperties>
</telerik:PropertyDefinition>
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>