This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have a list<> property in an object that initially starts with no items in it. It displays in the default property grid with 0items as the value, with a small + in a circle to the right of it. How do I go about enabling the ability to add items to the collection? |
|
|
I just realized it was adding them when I click the plus button, it just wasn't updating the property grid. |
|
|
Sorry if this sounds silly, but what actually happens when you click the + button? Does it just call the default constructor for the object it's a collection of? If so is there a way to control which constructor is called? Or even pass parameters to the constructor? |
|
|
If you change your List<> to ObservableCollection<> (or something else that implements INotifyCollectionChanged) then the grid will update automatically when things are added to the collection (whether using the + button or from your own code). Regarding object construction: yes, it just calls the default constructor. There is no direct way to override this, but you can interpose your own command binding for the CollectionAddCommand: pg.CommandBindings.Insert(0, new CommandBinding(PropertyGrid.CollectionAddCommand, OnCollectionAdd)); In your handler, e.Parameter is the collection that is being added to, so your handler will look something like this: private void OnCollectionAdd(object sender, ExecutedRoutedEventArgs e) Note you must use Insert so that your binding takes precedence over the built-in one, and that by doing this you get stuck with responsibility for handling all collection adds, which may be a pain if you have several child collections and only one of them needs special handling. (Call CollectionUtilities.AddDefaultValueEntry(collection) to fall back on the default behaviour.) If this is a significant issue for you then let us know your scenario and we'll see what we can come up with. |
|
|
Thanks Ivan, I'll have a play around with your suggestion. I'm not sure if we want to have to handle all collection adds though, but I'll experiment. I'm not sure it's a significant issue yet, we're still playing around with exactly how the interface for our app will function. A prior version of our product had customized dialogs for editing almost all object properties, we're giving the property grid a shot this time around just to see if it simplifies things. Whether or not it simplifies things depends mainly on these complex scenarios, we just have to figure out if the way it works is intuitive enough for the user. Could you do a kind of hack whereby you create a dummy collection similarly named, then whenever a new item is added display a dialog to get extended information from the user? Or what about just adding buttons that do other things to the property grid... If you could do that I could put an 'add widget' type button on that could then prompt the user for what is required then call whatever constructor etc is necessary.. |
|
|
You can actually do this pretty easily using the standard editor extensibility mechanisms and a bit of No. 8 wire. <DockPanel> You now need to reference the collection via the Value pseudo-property instead of it being the implicit data context. Hope this makes sense. You mentioned you were fairly new to WPF so things like markup extensions may be a bit unfamiliar to you but do feel free to ask for clarification if it's not clear what's going on here. |
|