绑定到 ObservableCollection时如何删除 DataGrid 的空白行?
我对此感到很抓狂:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WPF 4.0 版本的 DataGrid 中也存在同样的问题,这是由为 ObservableCollection ItemsSource 自动显示的添加新行引起的。将 IsReadOnly 设置为 True 太激进了恕我直言。
如果您不需要该行为,但您仍然希望修改单元格,我通过禁用 CanUserAddRows 属性解决了这个问题:
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:
我已经在 Datagrid XAML 上找到了它
,放置属性:
I've got it
on Datagrid XAML, put the attribute:
CanUserAddRows="False"
和IsReadOnly="True"
两者的组合可以更好地避免任何额外的不便。CanUserAddRows="False"
andIsReadOnly="True"
combination of both is better to ensure any additional inconveniences.