Search code examples
c#wpfdatatemplate

Extra rows in WPF Data Template


Extra rows in WPF DataTemplate Specification

This seems to be a really good overview of WPF Data Binding from Microsoft: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview

The page has links to a PDF download (which seems more comprehensive than the page itself): https://learn.microsoft.com/en-us/dotnet/opbuildpdf/framework/wpf/data/toc.pdf?branch=live and a sample application: http://go.microsoft.com/fwlink/?LinkID=163703.

Question:

In the sample application file: DataBindingLabApp.xaml (~ line 50) a data template is defined for individual list items. The template specifies 4 rows but only two rows are used... Is there a good reason for having unused rows?

   <DataTemplate DataType="{x:Type src:AuctionItem}">
        <Border BorderThickness="1" BorderBrush="Gray"
                Padding="7" Name="border" Margin="3" Width="500">
            <Grid>

                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>   <!-- not needed? -->
                    <RowDefinition/>   <!-- not needed? -->
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    ...

I deleted 2 of the rows and as far as I can tell (by comparing screen shots) the extra rows make no difference to the appearance of the list items. Is there any reason they should be retained? The only thing I can think of is for some future functionality (but that seems like a poor practice, no?)


Solution

  • Is there any reason they should be retained?

    Unless there are any items in the Grid that sets the Grid.Row attached property to either 2 or 3 you could remove them unless you want the top two (the only two) rows to fill only 50% of the Grid's total height.

    A RowDefinition has a default height of Auto which means that the available space will be divided equally among the number of <RowDefinition/> elements that you define.

    ...as far as I can tell (by comparing screen shots) the extra rows make no difference to the appearance of the list items. Is there any reason they should be retained?

    No.

    The only thing I can think of is for some future functionality (but that seems like a poor practice, no?)

    Yes, then you might as well add them later.