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
|
Below are few open questions on Properties grid control. 1. If the class that we are binding to the properties grid has few propeties that we don't want to display on the UI, how to get this? 2. If we want to have a dropdown for property of type list<DataType>. How to get it? (We should have the freedom to use any control for any property as per our requirement) 3. We need to have control over the sequence of the fields displayed on the UI. 4. How to remove the readonly grayyed field for the values sections for expand column.
Thanks, vishal. |
|
|
1. Use BrowsableAttribute(false) on the properties you want to hide. Or, if you don't control the class, you can use grid.BindingView.DefaultView.Filter. 2. Define a TypeEditor for List<DataType>. Note that due to restrictions on the XAML x:Type markup extension, you will need to either create a GenericTypeExtension or do this through code behind. I can provide examples if you need them. 3. See http://www.mindscape.co.nz/blog/index.php/2008/01/27/sorting-and-grouping-in-the-wpf-property-grid/ 4. Do you mean the read-only greyed field that is displayed for collections? If so see http://www.mindscape.co.nz/forums/Thread.aspx?PostID=1610. If you mean more generally for expandable properties, you can use a TypeEditor with AllowExpand="True" to create a summary (or blank) representation. See the Alloy window in the Templating sample for an example using PhoneNumbers. |
|
|
Ivan, thanks for your replies, this is very helpful. Could you please provide an ex to Vishal and I on 2? Thank you for your help.
|
|
|
To specify a generic type in XAML, you would need to write a class something like this: public class ListOfExtension : MarkupExtension Then in your XAML you can use this in a TypeEditor as follows: <ms:PropertyGrid> (Please pardon any syntax errors, I am typing directly into the forum here!) Alternatively, to install the TypeEditor from code-behind: TypeEditor myEditor = new TypeEditor(); |
|
|
Ivan, thanks for your replies, it is very helpful.
I am not able to get the implementation suggested for creating TypeEditor.
To specify a generic type in XAML, you would need to write a class something like this:
Class I created is like this.(List<User> where User is again a class and need to display userName in the dropdown) public class DropDownForListType : MarkupExtension{ private Type _type;public DropDownForListType(Type type){ _type = type; } public override object ProvideValue(IServiceProvider serviceProvider){ return typeof(List<User>).MakeGenericType(new Type[] { _type });} } Then how do we write the XAML for it. also please show the same from codebehind. Thanks,vishal. |
|
|
The problem is in your ProvideValue implementation. Type.MakeGenericType operates on what C# language geeks call an "open" generic type, i.e. one without the type parameters specified, such as List<>. But you are asking it to operate on a "closed" constructed type, because you are specifying List<User>. So you need to change your ProvideValue implementation to look like this: return typeof(List<>).MakeGenericType(new Type[] { _type }); // note no type parameter in List<> You should also change the name of your markup extension. All this is doing is giving you a way to name a generic type in XAML. It is not creating the drop-down editor or associating a type with the drop-down editor. So it should be called something like ListOf or ListOfExtension. Now you have the extension, you can refer to the List<User> type in XAML as follows: {local:ListOf {x:Type local:User}} (assuming the "local:" namespace prefix is mapped appropriately). So where does this get us? We still haven't told the grid that we want to use a drop-down to edit properties of type List<User>. But at least now we have a way to talk about the List<User> type in XAML. So finally we can get around to telling the grid what we want: <ms:PropertyGrid> where EditorTemplate is the DataTemplate containing whatever UI you want to use for editing user lists. (In this case a drop-down list.) The exact implementation of that DataTemplate will depend on what you want to do with the user list in the editor, e.g. just view it, or add and remove items, etc. etc., but it's just a normal DataTemplate. Note that the template will be binding to the List<User> object, so for example if you want to populate a drop-down from the list you will need to write something like: <DataTemplate x:Key="..."> Okay, what about doing the same thing code-behind? In this case, we don't need to muck around with XAML markup extensions because we can just name the List<User> type in code: TypeEditor userListDropDown = new TypeEditor(); Again, the exact implementation of the DataTemplate depends on what you want to do with the user list in the editor, but it will be the same as if you were adding the editor association in XAML. The easiest way to get at a DataTemplate from code-behind is to define the template in XAML in your Window.Resources section and call FindResource: userListDropDown.EditorTemplate = (DataTemplate)FindResource("MyUserListDropDown"); Does this clarify things? |
|