用空行填充 DataGrid [英] Fill up DataGrid with empty rows
问题描述
我有一个 DataGrid
绑定到一个 DataTable
.我希望 DataGrid
总是至少有十(空)行,如果没有足够的实际数据项(数据一点一点地进来).
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).
一种方法是在初始化时轻松地向 DataTable
添加十个空行.但是当一个真正的数据项进来时,我不能轻易添加该行,我必须找到第一个空行来覆盖它,这不是很方便.
那么有人知道一种更智能/内置的方法来实现这一目标吗?
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?
推荐答案
无论从哪个方面接近,都会一团糟.我会说你最好的选择(假设你的网格单元内容不会被包裹)是使用可视化画笔(带线条)作为你的 DataGrid 的背景.
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.
更新 1 - XAML它总是在那里,诀窍是使用 MinHeight,由于平铺背景,它会产生空白项目的视觉.如果您的网格将填充真实数据,背景将扩大,呈现更多线条.
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.
这是一个例子:
<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>
这篇关于用空行填充 DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!