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, Is there any way to dynamically add or remove properties after the PropertyGrid is displayed? When add a property, how could I add the property to a specified location? Thanks. |
|
|
There are two or three ways to do this: 1. Implement ICustomTypeDescriptor (though if you are changing the set of properties while the grid is displayed you may need some trickery to get it to pick up the changes). (You can also do this through TypeConverter.) 2. Use the AddNode method to manually add entries to the grid. This will not allow you to remove nodes that map to actual properties, though. 3. Instead of using properties, use the ItemsSource and the ObservableDictionary class to manage the set of entries. The best solution will depend on your exact scenario e.g. the balance between static and dynamic properties, where the dynamic properties are coming from, etc. Related thread: http://www.mindscape.co.nz/forums/Thread.aspx?ThreadID=1596 |
|
|
Thanks, ivan. When I used the AddNode method to add a new property, it showed a length subproperty under the new property. How could I hide the length subproperty? |
|
|
Is there any way to specify the position of the new property? I need the new property to be added under a certain existing property. Also, is there any way to specify the category and description for the new property? |
|
|
I don't think you specify the position directly, but you can use normal sorting techniques to order the properties. For example, you could emit the dynamic property with a custom attribute (see next paragraph), and create a SortDescription which respects that attribute. Also if you are using TypeConverter/ICustomTypeDescriptor then I think the grid will default to showing properties in the order you return them. For category and description you just need to emit the appropriate custom attributes on the dynamic property (i.e. override MemberDescriptor.Attributes in your custom PropertyDescriptor, and include a CategoryAttribute and DescriptionAttribute in the returned collection). |
|
|
What is the data type? If you are adding strings, you shouldn't be seeing that (we automatically hide the String.Length property). See AddNode.xaml.cs in the QuickStart sample. If you are adding values other than strings, we will by default show their subproperties. For example, if the value is an array, then you will see a Length subproperty. To hide subproperties, you must provide a custom editor. To do this, either: * declare a TypeEditor for the type of the value (in the <ms:PropertyGrid.Editors> element); or * create an editor template (a DataTemplate) and call the AddNode(string, object, DataTemplate) overload. Note the editor template can be as simple as a read-only TextBlock -- it doesn't have to actually support "editing" if you don't want it to. |
|
|
I use string data type. In the AddNode.xaml sample, it also showed the length subproperty, even though that was not expanded. Is there any sample to create data template in the source code when using the AddNode method? Is there any attribute we can set to hide the length subproperty without using data template?
|
|
|
In the SelectedObjectUsingICustomTypeDescriptor.xaml sample, seems the CategoryAttribute function doesn't work. There were no categories shown after the PropertyGrid was displayed. |
|
|
That's because the grids in the QuickStart sample don't have by-category grouping turned on. Add the following attribute to the <ms:PropertyGrid> element: Grouping="{x:Static ms:PropertyGrouping.ByCategory}" |
|
|
That's odd. I've just re-run the QuickStart / AddNode sample again, and it is NOT showing a Length subproperty in my tests. It certainly *shouldn't* show a Length property for strings: you should NOT need to create a DataTemplate just to hide the Length property for a string. Could you post the XAML file and the code which calls AddNode? (You can attach a file via the Options tab.) What version of .NET are you on -- 3.0, 3.0 SP1, 3.5, 3.5 SP1, 4.0? Thanks! There isn't a sample for AddNode with a DataTemplate but it's pretty simple. Define the data template in your XAML: <Window.Resources> And use FindResource to load it: DataTemplate dt = (DataTemplate)FindResource("dt"); But like I say this shouldn't be necessary to prevent the grid from showing String.Length... |
|
|
I use 3.5 sp1 with Visual Studio 2008 on Windows Vista. I tried the same code as in the AddNode.xaml sample file, and also ran your samples to test the 'Add Node' button. They both showed the Length subproperty. |
|
|
That's really strange. We can't reproduce this behaviour at all. Could you zip up the source code and compiled binaries and post them here (you can attach a zip file via the Options tab) for us to take a look at please? Thanks! |
|
|
Please find the zip file attached. |
|
|
Thanks for taking the time to provide us with this. It looks like this is a bug in the version of the property grid that you've got, which has since been fixed -- I can reproduce the problem using your DLL, but it behaves correctly with the latest nightly build. Please download the currently nightly build (you can get it from the Downloads tab), and do a clean and rebuild of the solution, and the issue should go away. We apologise for the inconvenience in tracking this down -- thanks again for taking the time to provide us with the repro sample. |
|
|
I downloaded the latest version of nightly build, cleaned and rebuilt the solution. But, it still showed the length subproperty. This length subproperty also exists in your latest version of samples. |
|
|
Is it possible to use the AddNode method to add a subproperty to an existing property? For example, I have a root property node called Area, and I need to dynamically add Width and Length subproperties to Area. If it is possible, is there any examples? If not, what method I could use? |
|
|
I'm afraid it is not currently possible to use AddNode to add a subproperty to an existing property. You might be able to do this by implementing a custom descriptor, but that has a quite different programming model from AddNode, so it would be a bit inconsistent if you're using AddNode (as opposed to a custom descriptor) to modify the top-level grid entries. If you could say a bit more about why you're wanting to do this then we would be happy to look at adding support for this feature (no promises though!). |
|
|
Our objects have children objects. We want to add properties dynamically to the children objects at the run time. Currently, every time when I add a new property with AddNode method, the new property goes to the end of the root level property list. Is there any way I can specify a certain position where I would like the new property to be? |
|