如何使用 DataGrid.CanUserAddRows = true 的工厂
我想使用 DataGrid.CanUserAddRows = true 功能。不幸的是,它似乎只适用于具有默认构造函数的具体类。我的业务对象集合不提供默认构造函数。
我正在寻找一种注册工厂的方法,该工厂知道如何为 DataGrid 创建对象。我查看了 DataGrid 和 ListCollectionView,但它们似乎都不支持我的场景。
I would like to use the DataGrid.CanUserAddRows = true feature. Unfortunately, it seems to work only with concrete classes which have a default constructor. My collection of business objects doesn't provide a default constructor.
I'm looking for a way to register a factory that knows how to create the objects for the DataGrid. I had a look at the DataGrid and the ListCollectionView but none of them seems to support my scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题:
“我正在寻找一种注册工厂的方法,该工厂知道如何为 DataGrid 创建对象”。 (因为我的业务对象集合不提供默认构造函数。)
症状:
如果我们设置 DataGrid.CanUserAddRows = true,然后将项目集合绑定到 DataGrid,但该项目不有默认构造函数,则 DataGrid 不会显示“新项目行”。
原因:
当项目集合绑定到任何 WPF ItemControl 时,WPF 会将该集合包装在以下任一位置:
a BindingListCollectionView(当绑定的集合是
BindingList
时)。BindingListCollectionView
实现 IEditableCollectionView 但不实现 <代码>IEditableCollectionViewAddNewItem。a ListCollectionView(当绑定的集合是任何其他集合时) 。
ListCollectionView
实现 IEditableCollectionViewAddNewItem (因此IEditableCollectionView
)。对于选项 2),DataGrid 将新项目的创建委托给
ListCollectionView
。ListCollectionView
在内部测试默认构造函数是否存在,如果不存在则禁用AddNew
。以下是使用 DotPeek 的 ListCollectionView 的相关代码。似乎没有一种简单的方法可以覆盖这种行为。
对于选项 1),情况要好得多。 DataGrid 将新项目的创建委托给 BindingListView,而 BindingListView 又委托给 BindingList。
BindingList
还会检查默认构造函数是否存在,但幸运的是BindingList
还允许客户端设置 AllowNew 属性并附加一个事件处理程序以提供一个新项目。稍后查看解决方案,但这里是BindingList
中的相关代码非解决方案:
期望 DataGrid 允许客户端附加一个回调,DataGrid 通过该回调请求默认的新项目,就像上面的
BindingList
一样。这将使客户在需要时能够首先创建新项目。不幸的是,即使在 .NET 4.5 中,DataGrid 也不直接支持这一点。
.NET 4.5 似乎确实有一个以前不可用的新事件“AddingNewItem”,但这只会让您知道正在添加新项目。
解决方法:
这种情况似乎不太可能,但想象一下实体框架创建的实体类没有默认构造函数(不太可能,因为它们不可序列化),那么我们可以简单地创建一个具有默认构造函数的分部类构造函数。问题解决了。
这里我们可以继承业务对象类型并添加默认构造函数。
最初这似乎是一个好主意,但转念一想,这可能需要比必要的更多的工作,因为我们需要将业务层生成的数据复制到业务对象的超类型版本中。
我们需要像这样的代码
,然后需要一些 LINQ 在这些对象的列表之间进行投影。
这要容易得多
现在我们需要做的就是使用一些 LINQ 在这些对象的列表之间进行投影,然后绑定到 DataGrid 中的
MyBusinessObject.BusinessObject
。不需要混乱的属性包装或值复制。解决方案:(找到一个)
BindingList
如果我们将业务对象集合包装在
BindingList
中,然后将 DataGrid 绑定到此,用几行代码我们的问题就解决了,DataGrid 将适当地显示一个新的项目行。其他解决方案
The problem:
"I'm looking for a way to register a factory that knows how to create the objects for the DataGrid". (Because my collection of business objects doesn't provide a default constructor.)
The symptoms:
If we set
DataGrid.CanUserAddRows = true
and then bind a collection of items to the DataGrid where the item doesn't have a default constructor, then the DataGrid doesn't show a 'new item row'.The causes:
When a collection of items is bound to any WPF ItemControl, WPF wraps the collection in either:
a BindingListCollectionView when the collection being bound is a
BindingList<T>
.BindingListCollectionView
implements IEditableCollectionView but doesn't implementIEditableCollectionViewAddNewItem
.a ListCollectionView when the collection being bound is any other collection.
ListCollectionView
implements IEditableCollectionViewAddNewItem (and henceIEditableCollectionView
).For option 2) the DataGrid delegates creation of new items to the
ListCollectionView
.ListCollectionView
internally tests for the existence of a default constructor and disablesAddNew
if one doesn't exist. Here's the relevant code from ListCollectionView using DotPeek.There doesn't seem to be an easy way to override this behaviour.
For option 1) the situation is a lot better. The DataGrid delegates creation of new items to the BindingListView, which in turn delegates to BindingList.
BindingList<T>
also checks for the existence of a default constructor, but fortunatelyBindingList<T>
also allows the client to set the AllowNew property and attach an event handler for supplying a new item. See the solution later, but here's the relevant code inBindingList<T>
Non-solutions:
It would reasonable to expect the DataGrid to allow the client to attach a callback, through which the DataGrid would request a default new item, just like
BindingList<T>
above. This would give the client the first crack at creating a new item when one is required.Unfortunately this isn't supported directly from the DataGrid, even in .NET 4.5.
.NET 4.5 does appear to have a new event 'AddingNewItem' that wasn't available previously, but this only lets you know a new item is being added.
Work arounds:
This scenario seems very unlikely, but imagine that Entity Framework created its entity classes with no default constructor (not likely since they wouldn't be serializable), then we could simply create a partial class with a default constructor. Problem solved.
Here we can inherit from the business object type and add a default constructor.
This initially seemed like a good idea, but on second thoughts this may require more work than is necessary because we need to copy data generated by the business layer into our super-type version of the business object.
We would need code like
And then some LINQ to project between lists of these objects.
This is much easier
Now all we need to do is use some LINQ to project between lists of these objects, and then bind to
MyBusinessObject.BusinessObject
in the DataGrid. No messy wrapping of properties or copying of values required.The solution: (hurray found one)
BindingList<T>
If we wrap our collection of business objects in a
BindingList<BusinessObject>
and then bind the DataGrid to this, with a few lines of code our problem is solved and the DataGrid will appropriately show a new item row.Other solutions
我找到了解决这个问题的另一种方法。就我而言,我的对象需要使用工厂进行初始化,并且实际上没有任何方法可以解决这个问题。
我无法使用
BindingList
,因为我的集合必须支持分组、排序和过滤,而BindingList
不支持。我通过使用 DataGrid 的
AddingNewItem
事件解决了该问题。这几乎完全没有记录的事件不仅告诉您正在添加一个新项目,而且允许让您选择要添加的项目。AddingNewItem
在其他任何事情之前触发;EventArgs
的NewItem
属性只是null
。即使您为该事件提供了处理程序,如果该类没有默认构造函数,DataGrid 也将拒绝允许用户添加行。然而,奇怪的是(但值得庆幸的是)如果您确实有一个,并且设置了 AddingNewItemEventArgs 的 NewItem 属性,那么它永远不会被调用。
如果您选择这样做,则可以按顺序使用
[Obsolete("Error", true)]
和[EditorBrowsable(EditorBrowsableState.Never)]
等属性以确保没有人调用构造函数。您还可以让构造函数主体抛出异常。反编译控件让我们可以看到其中发生了什么。
正如我们所看到的,在
4.5
版本中,DataGrid 确实使用了AddNewItem
。CollectionListView.CanAddNewItem
的内容很简单:因此,这并不能解释为什么我们仍然需要一个构造函数(即使它是一个虚拟的)才能显示添加行选项。我相信答案在于一些使用
CanAddNew
而不是CanAddNewItem
确定NewItemPlaceholder
行的可见性的代码。这可能被认为是某种错误。I've found another solution to this problem. In my case, my objects need to be initialized using a factory, and there isn't really any way to get around that.
I couldn't use
BindingList<T>
because my collection must support grouping, sorting, and filtering, whichBindingList<T>
does not support.I solved the problem by using DataGrid's
AddingNewItem
event. This almost entirely undocumented event not only tells you a new item is being added, but also allows lets you choose which item is being added.AddingNewItem
fires before anything else; theNewItem
property of theEventArgs
is simplynull
.Even if you provide a handler for the event, DataGrid will refuse to allow the user to add rows if the class doesn't have a default constructor. However, bizarrely (but thankfully) if you do have one, and set the
NewItem
property of theAddingNewItemEventArgs
, it will never be called.If you choose to do this, you can make use of attributes such as
[Obsolete("Error", true)]
and[EditorBrowsable(EditorBrowsableState.Never)]
in order to make sure no one ever invokes the constructor. You can also have the constructor body throw an exceptionDecompiling the control lets us see what's happening in there.
As we can see, in version
4.5
, the DataGrid does indeed make use ofAddNewItem
. The contents ofCollectionListView.CanAddNewItem
are simply:So this doesn't explain why we we still need to have a constructor (even if it is a dummy) in order for the add row option to appear. I believe the answer lies in some code that determines the visibility of the
NewItemPlaceholder
row usingCanAddNew
rather thanCanAddNewItem
. This might be considered some sort of bug.我查看了 IEditableCollectionViewAddNewItem 而且似乎正在添加此功能。
来自MSDN
尽管在 Bea Stollnitz 博客中,您可以阅读以下内容
这个答案来自 2009 年,所以也许它现在可用于 DataGrid
I had a look at IEditableCollectionViewAddNewItem and it seems to be adding this functionality.
From MSDN
Although at Bea Stollnitz blog, you can read the following
That answer is from 2009 so maybe it's usable for the DataGrid now
我建议最简单的方法是为您的类提供没有默认构造函数的包装器,其中将调用源类的构造函数。
例如,您的此类没有默认构造函数:
为其编写一个包装器:
Codebehind。
在您的 ViewModel 中,您需要为源集合创建包装器集合,它将处理数据网格中的项目添加/删除。
最后是 XAML 代码:
The simplest way I could suggest to provide wrapper for your class without default constructor, in which constructor for source class will be called.
For example you have this class without default constructor:
Write a wrapper for it:
Codebehind.
In your ViewModel you need to create wrapper collection for your source collection, which will handle item adding/removing in datagrid.
And finally, XAML code:
我只是想提供使用 BindingList 的替代解决方案。在我的情况下,业务对象保存在可移植项目 (Silverlight) 的 IEntitySet 中,该项目不支持 IBindingList。
首先,解决方案是对网格进行子类化,并覆盖 CanUserAddRows 的强制回调以使用 IEditableCollectionViewAddNewItem:
然后使用 AddingNewItem 事件来创建项目:
如果您关心细节,这就是它首先成为问题的原因。框架中的强制回调如下所示:
您看到它如何获取 IEditableCollectionView.CanAddNew 了吗?这意味着它仅在视图可以插入并构造项目时才启用添加。有趣的是,当我们想要添加新项目时,它会检查IEditableCollectionViewAddNewItem.CanAddNewItem,它只询问视图是否支持插入新项目(而不是创建):
I just wanted to provide an alternate solution to using a BindingList. In my situtation, the Business objects was held in an IEntitySet in a portable project (Silverlight), which did not support IBindingList.
The solution, first and foremost, is to subclass the grid, and overwrite the coerce callback for CanUserAddRows to use IEditableCollectionViewAddNewItem:
And then use the AddingNewItem event to create the item:
And if you care for the details, here is the reason why it is a problem in the first place. The coerce callback in the framework looks like this:
You see how it gets the IEditableCollectionView.CanAddNew? That means that it only enables adding when the view can insert and construct an item. The funny thing is that when we want to add a new item, it checks the IEditableCollectionViewAddNewItem.CanAddNewItem instead, which only asks if the view supports inserting new items (not creating):