WPF 数据网格底部的空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1783839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:14:49  来源:igfitidea点击:

WPF datagrid empty row at bottom

wpfdatagrid

提问by WtFudgE

I bind my datagrid using

我使用绑定我的数据网格

//fill datagrid
public DataTable GameData
{
    get
    {
        DataSet ds = new DataSet();
        FileStream fs = new FileStream(IMDB.WebPage.Class.Config.XMLPath,
        FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(fs, Encoding.Default);
        ds.ReadXml(reader);
        fs.Close();
        DataTable temp = ds.Tables[0];
        return ds.Tables[0];
     }
 }

For some reason I get an empty row at the bottom. And sometimes after clicking on some buttons and checkboxes in the grid, more empty rows are added.

出于某种原因,我在底部得到一个空行。有时在单击网格中的某些按钮和复选框后,会添加更多空行。

Why is this? And how do I block this?

为什么是这样?我该如何阻止?

回答by Tomi Junnila

Sounds like you probably have CanUserAddRowsset to true for the DataGrid. Just add

听起来您可能已将CanUserAddRowsDataGrid 设置为 true。只需添加

CanUserAddRows="false"

to the XAML.

到 XAML。

回答by Arti

It also works with the attribute:

它也适用于以下属性:

IsReadOnly="true"

回答by Alastair Maw

If your backing collection that implements IEditableCollectionViewreturns true from its CanAddNewmethod, the DataGrid will assume that is what you want to do.

如果实现的后备集合IEditableCollectionView从其CanAddNew方法返回 true ,则 DataGrid 将假定这就是您想要执行的操作。

There's a good overview here:Overview of the editing features in the WPF DataGrid

这里有一个很好的概述:WPF DataGrid 中编辑功能的概述

回答by Web Developer

If you're creating DataGrid on-the-fly through Source Code...

如果您正在通过源代码即时创建 DataGrid...

DataGrid grid = new DataGrid();

grid.CanUserAddRows = false;

//... 
grid.AutoGenerateColumns = false;
grid.Margin = new Thickness(10,20,10,10);
grid.VerticalAlignment = VerticalAlignment.Top;
grid.ItemsSource = //... and so on

回答by IronRod

Though the OP was asking how to REMOVE the empty row, the title isn't specific, and this article appeared in my search while trying to figure out how to ADD the empty row. I found that, for the empty row to appear, it not only needs to have CanUserAddRows="True"but the ItemsSourceneeds to have a default constructor public MyClass () { }.

虽然 OP 询问如何删除空行,但标题并不具体,这篇文章出现在我的搜索中,同时试图找出如何添加空行。我发现,要显示空行,它不仅需要具有,CanUserAddRows="True"ItemsSource需要具有默认构造函数public MyClass () { }