Quantcast
Channel: wpftoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 2157

New Post: PropertyGrid - Programmatically registering a custom editor

$
0
0
Hi there,

I'm trying to build a custom editor for a property getting added in to a PropertyGrid instance, but haven't been able to register it correctly.

My custom editor, TextureEditor creates a Grid with a text box and a button :
class TextureEditor : ITypeEditor
{
public FrameworkElement ResolveEditor( PropertyItem aPropertyItem )
{
        // Create a grid component
        Grid editorGrid = new Grid();
        editorGrid.ColumnDefinitions.Add( new ColumnDefinition() );
        editorGrid.ColumnDefinitions.Add( new ColumnDefinition() );

        // Create a TextBox to display the value of the texture
        TextBox textBox = new TextBox();
        Grid.SetColumn( textBox, 0 );
        Grid.SetRow( textBox, 0 );
            
        // Create a button to launch the TextureBrowser
        Button launchEditor     = new Button();
        Grid.SetColumn( launchEditor, 1 );
        Grid.SetRow( launchEditor, 0 );
        launchEditor.Content    = "Select Texture";
            
        // Create the data binding
        Binding dataBinding = new Binding( "Value" );
        dataBinding.Source  = aPropertyItem;
        dataBinding.ValidatesOnExceptions = true;
        dataBinding.ValidatesOnDataErrors = true;
        dataBinding.Mode = aPropertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
        BindingOperations.SetBinding( textBox, TextBox.TextProperty, dataBinding );
                    
        // Add the components to the grid
        editorGrid.Children.Add( textBox );
        editorGrid.Children.Add( launchEditor );

        return editorGrid;
 }
}
The property I'm trying to register this editor for is a TextureID, and it comes from an object that I can't define an editor attribute for because it's in a library that doesn't have any UI elements.

So I've tried two ways, first off programmatically, where myPropertyGrid is the PropertyGrid instance :
EditorDefinition    textureEditorDefinition = new EditorDefinition();
textureEditorDefinition.TargetType          = typeof(string);

PropertyDefinition  textureProperty      = new PropertyDefinition();
textureProperty.Name                        = "TextureID";
            
DataTemplate        textureTemplate      = new DataTemplate();
textureTemplate.DataType                    = typeof(TextureEditor);

textureEditorDefinition.PropertiesDefinitions.Add( textureProperty );
textureEditorDefinition.EditorTemplate = textureTemplate;

myObjectPropertyGrid.EditorDefinitions.Add( textureEditorDefinition );
The new texture editor is never used when an object with a TextureID property is assigned to it.

I've also tried to add my TextureEditor in to the xaml (after adding the editors namespace, calling it custom):
<xctk:PropertyGrid.EditorDefinitions>
                            <xctk:EditorDefinition>
                                <xctk:EditorDefinition.PropertiesDefinitions>
                                    <xctk:PropertyDefinition Name="TextureID" />
                                    <xctk:PropertyDefinition Name="EffectID"/>
                                </xctk:EditorDefinition.PropertiesDefinitions>
                                <xctk:EditorDefinition.EditorTemplate>
                                    <DataTemplate>
                                        <custom:TextureEditor />
                                    </DataTemplate>
                                </xctk:EditorDefinition.EditorTemplate>
                            </xctk:EditorDefinition>
But I get a compile error saying that only FrameworkElement and FrameworkContentElement types are valid.

Any ideas? I guess I must be missing something when setting it up programmatically, but can't figure out what it is.

Viewing all articles
Browse latest Browse all 2157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>