WPF 中的可编辑 DataGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1423655/
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
Editable DataGrid in WPF
提问by David Brunelle
I have a program where I need the user to input data in a DataGrid. I though the simple act of setting the 'CanUserAddRows' would be enough to let the user add new rows, but it seem that it won't cut it.
我有一个程序,我需要用户在 DataGrid 中输入数据。我虽然设置 'CanUserAddRows' 的简单行为足以让用户添加新行,但它似乎不会削减它。
Is there something else I need to define to make them work? My DataGrid has ComboBoxes and TextBoxes in them so it's pretty common controls.
还有什么我需要定义才能使它们工作吗?我的 DataGrid 中有 ComboBoxes 和 TextBoxes,所以它是非常常见的控件。
The code I have so far is this
我到目前为止的代码是这样的
<dg:DataGrid Name="GridFournisseur" ItemsSource="{Binding}"
Margin="423,41,23.5,0" Height="193" VerticalAlignment="Top"
CanUserAddRows="True" CanUserDeleteRows="True" IsTabStop="True" RowHeight="12" SelectionUnit="CellOrRowHeader">
<dg:DataGrid.Columns>
<dg:DataGridComboBoxColumn Header="Fournisseur" Width="*" MinWidth="150"
IsReadOnly="False" />
<dg:DataGridTextColumn Header="Prix" Width="SizeToHeader" MinWidth="50"
Binding="{Binding Categorie}"
IsReadOnly="False"/>
<dg:DataGridTextColumn Header="Délai" Width="SizeToHeader" MinWidth="50"
Binding="{Binding NoPiece}"
IsReadOnly="False"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
I just have this DataGrid in which I would like to edit its content and at the beginning it's empty.
我只有这个 DataGrid,我想在其中编辑它的内容,但一开始它是空的。
采纳答案by Szymon Rozga
It could be any of a number of things. Please take a look at this article.
它可以是多种事物中的任何一种。请看一下这篇文章。
The problem is most likely that you are binding to a collection view that does not support adding items. I believe that the grid might be expecting a collection view that implements IEditableCollectionViewor IBindingList, interfaces that support adding items.
问题很可能是您绑定到不支持添加项目的集合视图。我相信网格可能需要一个实现IEditableCollectionView或IBindingList的集合视图,这些接口支持添加项目。
回答by Taylor Leese
CanUserAddRows
is the correct property. Perhaps share some sample code.
CanUserAddRows
是正确的属性。也许分享一些示例代码。