wpf - DataGrid表显示额外的不必要的空行

标签 wpf xaml mvvm datagrid

我使用以下代码,例如,当集合是带有数据的字段,例如我在屏幕上看到的2个项目时,在列表的末尾有2行带有数据和一行空行。

如何省略最后一个空行?该集合只有2个条目。收集项目只有2个条目。

<DataGrid RowHeaderWidth="0" ItemsSource="{Binding Items}" AutoGenerateColumns="False">
    <!--<DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" />
        </DataGrid.Resources>-->
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="1" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Column1, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTextColumn Binding="{Binding Column2}" Header="2" Width="*" />
        <DataGridTextColumn Binding="{Binding Column3}" Header="3" Width="*" />
        <DataGridTextColumn Binding="{Binding Column4}" Header="4" Width="*" />
        <DataGridTextColumn Binding="{Binding Column5}" Header="5" Width="*" />
    </DataGrid.Columns> 
</DataGrid>

最佳答案

尝试将 CanUserAddRows 设置为false,注册的默认值为true:

<DataGrid Name="MyDataGrid" 
          CanUserAddRows="False" ... />

引用 MSDN :

When this property is set to true a blank row is displayed at the bottom of the DataGrid. A user can enter a new item into the blank row. Adding a new row adds an item to the ItemsSource.

关于wpf - DataGrid表显示额外的不必要的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21660935/

相关文章:

c# - 导致 xamDataGrid 重新绘制

mvvm - DataGrid.RowDetailsTemplate 使用访问命令方法的麻烦

c# - WPF/MVVM - 检查基于字符串匹配的 MenuItem

c# - 如何获取用星号定义的 WPF 网格列来剪辑内容?

c# - 如何将不受支持的模式添加到 AutomationElement? AutomationPeers 会更改被测应用程序吗?

c# - WPF:DataGrid 在值更改时排序

wpf - 如何像气泡一样设置WPF工具提示的样式?

c# - 使用 MVVM 模式将项目添加到集合

wpf - 使用鼠标事件获得 ScrollViewer 的 PanningMode 行为

c# - 将样式中的 Setter 值绑定(bind)到主模型