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
|
Hello, I am trying to find an example about how to edit collection property. For example, I have an object called MyObject. It has a property called Cars, which is a collection. The objects in this collection could be from any of the following classes: Ford, Honda or Toyata. Can your property grid allow user to choose a class to create its object and added to the collection. Further more it should be able the edit the properties on the added object. Do you have an example about how to do that? Thanks, Yang |
|
|
The built-in collection editor will only create objects of the declared type of the collection. For example, if Cars is a List<Car>, then clicking the Add button will create a Car. The user can then edit the properties on the newly added Car by expanding the collection. To create an Add button which prompts the user for the type of car and creates an object of a different type depending on the answer, create a custom editor. This could consist of a TextBlock showing the number of cars in the collection and a button whose Click event handler displays a prompt, creates an instance and adds it to the collection. You can use Tag or an attached property on the button to get a reference to the collection (see the discussion on ellipsis buttons which you have already found), or you can use Button.Command instead of Click, and databind CommandParameter to the collection. Note that, as usual with bound collections, the Cars collection must implement INotifyCollectionChanged in order for the grid to pick up new elements. Also, remember to set AllowExpand="True" on the editor declaration so that the user can still expand the collection. Hence something like: <DataTemplate x:Key="CarsCollectionEditor"> <ms:PropertyGrid.Editors> private void AddCarButton_Click(object sender, EventArgs e) (Typed directly into forums so apologies for any syntax errors!) |
|