Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

wpf - Fill up DataGrid with empty rows

I have a DataGrid bound to a DataTable. I want the DataGrid to always have at least ten (empty) rows, also if there are not enough real data-items (the data comes in little by little).
Example

One approach would be to easily add ten empty rows to the DataTable at initialization. But when a real data-item comes in, I can't easily add the row, I have to find the first empty row to overwrite it, what is not very handy.
So someone knows a smarter/built-in way to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's gonna be mess, no matter from what side it's approached. I'd say your best bet (provided that your grid cells content won't be wrapped) is to use a visual brush (with lines) as your DataGrid's background.

UPDATE 1 - XAML It's alwast there, the trick is to use MinHeight, which will produce a vision of blank items thanks to tiled background. If your grid will be populated with the real data the background will expand, rendering more lines.

One thing I didn't try is how it'll handling scrolling.

Here's an example:

<Grid>
    <Grid.Resources>
        <VisualBrush x:Key="StripesBrush" TileMode="Tile" Viewport="0,0,5,20"
               Viewbox="0,0,10,10" ViewportUnits="Absolute" 
               ViewboxUnits="Absolute">
            <VisualBrush.Visual>
                <Line X1="0" X2="10000" Y1="0" Y2="0" Stroke="DarkGray"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Grid.Resources>
    <DataGrid x:Name="g" AutoGenerateColumns="False" 
              GridLinesVisibility="None" 
              MinHeight="100"
              Height="100"
              VerticalAlignment="Top"
              Background="{StaticResource StripesBrush}">

        <DataGrid.Columns>
            <DataGridTextColumn Header="A" Width="Auto" Binding="{Binding Item1}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="Transparent"></Setter>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="B" Width="Auto" Binding="{Binding Item2}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...