C# WPF 数据网格允许用户添加行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2060046/
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
WPF datagrid allow user to add rows?
提问by JP Richardson
I would like my WPF Datagrid that is bound to my observable collection to have the blank row at the bottom so that the user can add more info. I've successfully bound the data, i.e. I can see it.
我希望绑定到我的可观察集合的 WPF Datagrid 在底部具有空白行,以便用户可以添加更多信息。我已经成功绑定了数据,即我可以看到它。
Why is the 'new' blank row not showing? Here is my xaml declaration:
为什么不显示“新”空白行?这是我的 xaml 声明:
<UserControl.Resources>
<CollectionViewSource x:Key="MyItems" Source="{Binding Path=AllItems}">
</CollectionViewSource>
</UserControl.Resource>
<my:DataGrid HorizontalAlignment="Stretch"
AutoGenerateColumns="True"
SelectionUnit="FullRow"
CanUserAddRows="True"
CanUserDeleteRows="True"
DataContext="{StaticResource MyItems}"
ItemsSource="{Binding}">
PS: I'm using Josh Smith's MVVM implementation. I have also read some SO postson the issue and theyhave not helped.
PS:我正在使用Josh Smith 的 MVVM 实现。我还阅读了一些关于这个问题的SO帖子,但它们没有帮助。
Thanks in advance.
提前致谢。
Update 2010-01-14:
2010-01-14 更新:
When the usercontrol load event occurs, "CanUserAdddRows" is false. I suspect is has something to do with the conditions listed here.
当用户控件加载事件发生时,“CanUserAdddRows”为false。我怀疑这与此处列出的条件有关。
采纳答案by JP Richardson
Found the problem. My constructor in the object that's part of my ObservableCollection wasn't declared public.
发现问题了。作为 ObservableCollection 一部分的对象中的构造函数未声明为公开的。
*Hits head*
*撞头*
Thanks for your time.
谢谢你的时间。
回答by Aran Mulholland
The new blank row will depend if the collection you are binding implements IEditableCollectionView. see here. That being said i have found that sometimes it works well and other times unexplainably it seems not to, but the IEditableCollectionView is a start
新的空白行将取决于您绑定的集合是否实现 IEditableCollectionView。看到这里。话虽如此,我发现有时它运行良好,有时却无法解释它似乎不起作用,但 IEditableCollectionView 是一个开始
回答by bartonm
Make sure your objects in the ObeservableCollection have a default parameterless constructor.
确保 ObeservableCollection 中的对象具有默认的无参数构造函数。