tags:

views:

279

answers:

1

How does ListCollectionView.AddNew determine the type of object it creates, and how could one affect it?

I have a hierarchy of a few types (Base, DerivedA, and DerivedB), and currently my WPF Toolkit DataGrid creates DerivedA objects (why, I don't know -- probably because almost all the data in the grid is of that type), but I'd like it to create DerivedB objects instead.

Update: I've tried deriving a new class from ListCollectionView and implementing a new AddNew method for it, and now I'm almost there: the only remaining problem is that after adding a new item, a new new item placeholder isn't added, so I can only add one item. My current approach looks somewhat like this:

public class CustomView : ListCollectionView, IEditableCollectionView
{
    public CustomView(System.Collections.IList list)
        : base(list)
    {
    }

    object IEditableCollectionView.AddNew()
    {
        DerivedB obj = new DerivedB();
        InternalList.Add(obj);
        return obj;
    }
}
+1  A: 

TomiJ,

see if it helps, but isn't the answer ok?

http://www.cnblogs.com/winkingzhang/archive/2008/05/22/1204581.html

Kamia
The article (the original one is at <http://blogs.msdn.com/vinsibal/archive/2008/05/20/wpf-3-5-sp1-feature-ieditablecollectionview.aspx>) was of some help, as it did set me off looking at `ListCollectionView`.
TomiJ
I see, i you managed to get the right answer, don't forget to update here.It's a very interesting question :D
Kamia
I'm not quite there yet, as I can only add _one_ new item, but at least the correct AddNew method gets called. I'll have to figure out what else I need to implement to get proper functionality.
TomiJ
why don't you implement a strategy pattern, are your familiar with the concept?
Kamia
Where do you mean I should apply the strategy pattern at? Basically add a new Context class which would contain the Base/DerivedA/DerivedB instances and the collection would then contain these? This is a bit awkward.
TomiJ