Telerik blogs

For my first blog post I’m going to present a little pet project inspired by requests from some of our customers. And so, without further ado:breadcrumb1

The Breadcrumb is a simple control that analyzes the type of the collection found in the RadGridView’s ItemsSource property and automatically resolves data hierarchies.

Usage is pretty straight-forward. Place the control anywhere you like and then connect it to the RadGridView, all in XAML. Example:

<bread:Breadcrumb Grid.Row="0" Margin="5" SourceGridView="{Binding ElementName=radGrid}"/>

The logic behind the type analysis algorithm is simple: represent every collection as a generic IEnumerable<> and get the type of the generic argument. So if you have an array of objects of type SalesOrder in a property of the business object you use, the Breadcrumb is going to allow you to expand the row for the business object and reveal a nested grid displaying the related SalesOrder objects. And all this without coding the hierarchy relationships in XAML or code-behind.

The actual magic happens in the following method:

internal static List<DataObject> BuildDataObjectTree(Type dataType)
       
{
           
var objects = new List<DataObject>();

           
foreach (var propInfo in dataType.GetProperties())
           
{
               
var genericArgument = propInfo.PropertyType.GetEnumerableGenericArgument();
               
if (genericArgument != null)
               
{
                   
objects.Add(new DataObject
                   
{
                       
Name = propInfo.Name,
                       
Type = dataType,
                       
Objects = BuildDataObjectTree(genericArgument)
                   
});
               
}
           
}

           
return objects;
       
}

This method is a recursive function that first gets called on the type of the items in the RadGridView’s ItemsSource property. It cycles trough the properties of the supplied type to see if any of them is actually a collection and goes on until it finds a type without a property of type IEnumerable<>. Its purpose is to build a tree, which is later used to dynamically generate the hierarchy within the RadGridView.

You can grab the source code of the sample application (data courtesy of Blend Sample Data Inc.) and the control itself here.


Related Posts

Comments

Comments are disabled in preview mode.