Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

How to remove DataGrid's blank row when binding to a ObservableCollection<T>?

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?

question from:https://stackoverflow.com/questions/1337258/how-to-remove-datagrids-blank-row-when-binding-to-a-observablecollectiont

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...