绑定到 ObservableCollection时如何删除 DataGrid 的空白行?

发布于 2024-08-02 13:02:08 字数 643 浏览 0 评论 0原文

我对此感到很抓狂:

ObservableCollection<Employee> list = new ObservableCollection<Employee>();
dgEmployees.ItemsSource = list;

当您调试列表变量时,它是空的(list.Count = 0),但是当我将它绑定到DataGrid(WPFToolkit)时,它显示了一个空白行。

在立即窗口中,对于 dgEmployees.Items 它显示:

dgEmployees.Items[0]
{NewItemPlaceholder}

并且

dgEmployees.Items[0].GetType()
{Name = "NamedObject" FullName = "MS.Internal.NamedObject"}
[System.RuntimeType]: {Name = "NamedObject" FullName = "MS.Internal.NamedObject"}

似乎是在我将此 Datagrid 放入 TabControl 后发生的,但我不确定它是否与之有关。

有谁知道如何删除这个空白行?

I'm getting nuts here with this:

ObservableCollection<Employee> list = new ObservableCollection<Employee>();
dgEmployees.ItemsSource = list;

When you debug the list variable, it's empty (list.Count =0), but then I bind it to a DataGrid (WPFToolkit), it shows me a blank row.

In immediate window, for dgEmployees.Items it's showing:

dgEmployees.Items[0]
{NewItemPlaceholder}

and

dgEmployees.Items[0].GetType()
{Name = "NamedObject" FullName = "MS.Internal.NamedObject"}
[System.RuntimeType]: {Name = "NamedObject" FullName = "MS.Internal.NamedObject"}

It seems to happen after I've put this Datagrid into a TabControl, but I'm not sure it has something to do with it.

Does anyone know how to remove this blank row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔姬 2024-08-09 13:02:08

WPF 4.0 版本的 DataGrid 中也存在同样的问题,这是由为 ObservableCollection ItemsSource 自动显示的添加新行引起的。将 IsReadOnly 设置为 True 太激进了恕我直言。
如果您不需要该行为,但您仍然希望修改单元格,我通过禁用 CanUserAddRows 属性解决了这个问题:

CanUserAddRows="False"

The same problem persist in WPF 4.0 version of DataGrid, and it is caused by the add-new row which it shows automatically for ObservableCollection ItemsSource. Setting IsReadOnly as True it's too radical IMHO.
I solved it by disabling CanUserAddRows property if you don't need that behavior, but you still want cells to be modified:

CanUserAddRows="False"

酒中人 2024-08-09 13:02:08

我已经在 Datagrid XAML 上找到了它

,放置属性:

IsReadOnly="True"

I've got it

on Datagrid XAML, put the attribute:

IsReadOnly="True"
慕烟庭风 2024-08-09 13:02:08

CanUserAddRows="False"IsReadOnly="True" 两者的组合可以更好地避免任何额外的不便。

CanUserAddRows="False" and IsReadOnly="True" combination of both is better to ensure any additional inconveniences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文