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
|
Dear All, In the attached sample i have added Button and ListBox controls to Window, I am trying to change Content Thanks in advance.
|
|
|
This happens because the property grid uses the declared property types to locate editors, and WPF provides only very weak type information for Button.Content and ListBox.Items. Button.Content is typed as object (not string). So the property grid has no idea what kind of editor to present for this (what kind of editor can handle *any* .NET object?!) and falls back on its default presentation, which is a read-only display. You can deal with this by telling the grid what kind of editor to use by specifying a PropertyEditor, e.g. <ms:PropertyEditor DeclaringType="Button" PropertyName="Content" EditorTemplate="{StaticResource {x:Static ms:PropertyGrid.SimpleTextEditorKey}}" />. ListBox.Items is typed as ItemCollection, meaning we cannot determine the type of the elements. So we don't allow adding items because we wouldn't know what kind of item to add when the user clicked the Add button. (Strings? Points? Llamas?) Again you could create a custom property editor for Items, with your own Add button. (Set AllowExpand="True" so that the collection remains expandable.) Alternatively, if you could give us more information about your scenario then we could probably add a "hinting" feature that would allow you to work with typeless collections such as ListBox.Items. Finally, depending on what you are trying to do, a better approach may be to have a view model, and bind both the ListBox.ItemsSource and the PropertyGrid.SelectedObject to the same underlying view model object. |
|
|
Thanks Ivan. It solved the button issue. Can you please help / suggest us in creating a simple string Items editor for a listbox? Or if there is one available point it for us. Really appreciate your time. Life is so beautiful!!! Success, Niraj |
|
|
Hello Niraj, Here is an example of an ItemCollection editor that will populate the collection with strings. This demonstrates only functionality -- you would probably make it look a bit nicer! Also, the use of Tag is not particularly clean and you might want to tidy this, e.g. using a Command and CommandParameter rather than a Click event. First the data template: <DataTemplate x:Key="ItemCollectionStringEditor"> And the property grid markup: <ms:PropertyGrid.Editors> And the magic is in the button event handler: private void AddStringButton_Click(object sender, RoutedEventArgs e) Important thing to notice: the click handler is adding String.Empty rather than null. This is because, if you add null, then we can't display an editor, because ItemCollection doesn't give us the type information, and we can't infer it from the type of the value (because null could be of almost any type). (There is currently a bug where if an untyped collection contains null, the grid will throw an exception when you expand the collection; this will be fixed shortly, but you'll still end up unable to edit the value.) By adding String.Empty instead, the grid can now figure out that this collection element is a string and display the appropriate editor. Obviously you can easily modify this code to add a different type of object just by replacing String.Empty with a suitable value, e.g. items.Add(42), or for reference types a constructor call, e.g. items.Add(new Llama()), and you could extend the code to have multiple buttons which would add different kinds of object to the collection. |
|