This article is compatible with the latest version of Silverlight.
It is time for my next article about the DataGrid control in Silverlight. This time I will focus on the row details template, used to
present additional information related to the whole row. We can always use an additional column to display this information, but using the row details has its advantages, such as: the row details doesn't affect the dimensions of the row and the cells in it, it can be shown only for a particular row and is bound to it.
Download source code
Adding row details to a DataGrid row
To create row details for our DataGrid the only thing we have to do is to set the RowDetailsTemplate property to an appropriate template. As you know there are no limitations for the template, so we can put whatever controls we like in it, just be sure to layout your controls properly by using hosting panel.
<my:DataGrid x:Name="Foods" Height="550" AutoGenerateColumns="False">
<my:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="White">
<Image Source="logo.png" Margin="5,5,5,5"></Image>
<TextBlock Margin="5,5,5,5"
Width="350"
Text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum varius.
Nullam felis purus,
lacinia at, molestie non, porttitor sit amet, lectus. Fusce molestie,
diam ac rutrum faucibus,
nulla diam luctus."
TextWrapping="Wrap">
</TextBlock>
</StackPanel>
</DataTemplate>
</my:DataGrid.RowDetailsTemplate>
<my:DataGrid.Columns>
...
</my:DataGrid.Columns>
</my:DataGrid>
The grid row and the controls in the row details share one and the same DataContext, so they can be also bound. For example the Text property of the TextBlock can be bound to the Description property of the DataContext object.
Row details visibility
With the property RowDetialsVisibilityMode we can change the visibility of the row details. It has three values:
When set to Collapsed the row details are never shown, Visible makes the row details for each row visible all the time and VisibleWhenSelected displays the row details for the selected row only. The default value is VisibleWhenSelected.
The RowDetailsVisibilityChanged event
This event is raised when a change to the visibility of the row details is made. For example if we have DataGrid with RowDetailsVisibilityMode set to VisibleWhenSelected, the event is raised when the row is selected and the row details are shown. Note that this event is also raised when the row details of the previously selected row are hidden.
Summary
That was a pretty short and simple article, but the row details feature of the DataGrid is really important and powerful one. So even if it looks easy, it gives us the possibility to add a great interactivity and functionality to the DataGrid.
If you have any questions or comments, please use the comments section bellow.
Thank you.