英文:
No Add Rows Operation allowed when a DataGrid binds to a list or observable collection of Double
问题
我有一个在我的ViewModel中定义的ObservableCollection<double>
。
ListWidthsFlat=new ObservableCollection<double>();
ListWidthsFlat.Add(120);
ListWidthsFlat.Add(200);
我的XAML代码:
<DataGrid ItemsSource="{Binding Path=ListWidthsFlat}" AutoGenerateColumns="False"
CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static p:Resources.Width}" Binding="{Binding ., UpdateSourceTrigger=PropertyChanged}" Width="100" />
</DataGrid.Columns>
</DataGrid>
我想要的是显示ObservableCollection,然后提供添加/删除项到我的ObservableCollection<double>
的可能性。
当我对ObservableCollection<T>
做同样的事情时,一切都正常工作。但是当绑定到ObservableCollection<double>
时,似乎参数CanUserAddRows
不起作用。
编辑:
经过额外的测试,似乎问题是当我将DataGrid绑定到ObservableCollection<T>
时,设置CanUserAddRow=True
,会自动创建一个额外的空行(这样我可以编辑它并向ObservableCollection添加新项)。
当我将DataGrid绑定到ObservableCollection<double>
时,不会创建空行。
这是一个截图,以便更容易理解:
英文:
I have an ObservableCollection<double>
that is defined in my ViewModel.
ListWidthsFlat=new ObservableCollection<double>();
ListWidthsFlat.Add(120);
ListWidthsFlat.Add(200);
My XAML code :
<DataGrid ItemsSource="{Binding Path=ListWidthsFlat}" AutoGenerateColumns="False"
CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static p:Resources.Width}" Binding="{Binding ., UpdateSourceTrigger=PropertyChanged}" Width="100" />
</DataGrid.Columns>
</DataGrid>
What I want is show the ObservableCollection, then offer possibility to add/delete items from my ObservableCollection<double>
.
When I do the same thing on an ObservableCollection<T>
all is working perfectly.
But when binding to ObservableCollection<double>
, seems that parameters CanUserAddRows
is not working.
Edit :
After additional tests, it seems the problem is that when I bind a DataGrid to an ObservableCollection<T>
, and set CanUserAddRow=True
, an additional empty line is automatically created (so I can edit it and add a new item to ObservableCollection.
When I bind a DataGrid to ObservableCollection<double>
, no empty line is created.
Here is a screenshot to make it more understandable :
答案1
得分: 2
为了正确执行CanUserAddRows
,绑定到的对象列表必须实现IEditableCollectionView Interface,该接口为绑定的集合提供了基本的编辑功能。在其中,从列表中呈现的项目必须具有公共的无参数构造函数。
由于值类型double
没有构造函数,因此网格会检测到这一点,并且不提供添加行;因此,你仅在double
上看到失败,但它适用于对象(类)的实例,这些实例具有List<T>
作为特定属性。
为了解决这个限制,
- 创建一个具有公共无参数构造函数和一个
double
属性的类。 - 然后创建您的类列表,并将其绑定到
ObservableCollection
(或者如果您不需要observablecollection的开销,也可以使用List
),并设置一组您的值。 - 在Xaml中,将数据网格中的列指向
double
属性。 - 您可能需要编写一个值构造函数,该构造函数将接受一个浮点数并返回一个字符串,并将字符串转换为浮点数。
英文:
To properly do a CanUserAddRows
the object list being bound to must implement IEditableCollectionView Interface which provide basic editing capabilities to the collection being bound to. Within that the item being presented from the list has to have a public default parameterless constructor.
Because a value type double
does not have a constructor the grid detects that and does not provide an add row; hence you see the failure on double
alone, but it works on the object, (class) instances of List<T>
which have double as a specific property.
To work around the limitation,
- Create a class which has a public parameterless constructor and one
double
property. - Then create your list of class and bind to that
ObservableCollection
(orList
works too actually if you don't need the overhead of the observablecollection.) with a set of your values. - In Xaml set the column in the datagrid to point to the double property.
- You may need to write a value constructor which will take in a float and return a string, and convert a string to a float.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论