The main goal of the Row Details feature is to let you present additional information about a row. This makes row details the perfect candidate for presenting hierarchical data.
My sample uses two kinds of business objects – a football club and its players. As you might expect each club has some players. The master grid will show our clubs and when the user wants to see some additional information about a club he will be presented with the row details. Our first goal is to define what will these details look like. We would like to see some general information about the club and its current squad (or part of it for the sake of the example).
To do that we will create a UserControl containing an Image and another RadGridView. As you might have guessed, this last grid will be showing the club’s squad. The Image will show the logo of the club. Here is what the control looks like:
Next we need to tell the grid to use this control to display its row details. Simply set its RowDetailsTemplate like this:
Now we are almost ready. Typically, you would want the row details to show up when a user selects a row. This is exactly what the RowDetailsVisibilityMode property of the grid takes care of. The GridViewRowDetailsVisibilityMode enumeration has three values – Collapsed, Visible and VisibleWhenSelected which do exactly what their names suggest.
We, however, would like to go a step further. If you are not content with the grid governing the row details behavior of all rows you can redefine this for each and every row. Each GridViewRow has a property called DetailsVisibility which overrides the behavior imposed by the grid. The mechanism is simple – if this property is null then the row obeys its parent grid about showing and hiding row details. Otherwise it is what it is. We will use this to create a custom column with a toggle button that will show / hide the row details.
To do that we will inherit from GridViewColumn and override the CreateCellElement method. There we will return our “special” toggle button that knows about its parent GridViewRow and controls the DetailsVisibility:
The button is very simple as well. It has its IsChecked property bound to the DetailsVisibility property of its parent row with the help of the almighty BooleanToVisibilityConverter:
Well, I lied. The designer mate sitting next to me saw the toggle button and styled it in Blend to give it this finished look, but you can examine this in the source code bundle.
And that’s all about it. Here is our grid with a “toggle hierarchy row details”:
Here is the Source Code
Subscribe to be the first to get our expert-written articles and tutorials for developers!