Click here to Skip to main content
15,892,161 members

WPF PropertyGrid with Custom Editor

#realJSOP asked:

Open original thread
I'm using the WPF Extended Toolkit PropertyGrid (v1.7), and I'm trying to setup a custom editor to display a dialog box when the user clicks a button. My problem is that I can't get the property grid to display the editor panel at all.

The property in question is properly adorned with the following attributes (and the property itself does show up in the propertygrid):
C#
[Editor(typeof(CDSceneSelectorEditor), typeof(DialogPropertyValueEditor))]
[Category("General")]
[DisplayName("Base Scene")]
[Browsable(true)]
public Image BaseSceneImage { get; set; }

I tried doing it as follows, but it wouldn't display the editor panel, and after some investigation in the debugger, I noticed that the template's VisualTree was null:
C#
public class CDSceneSelectorEditor : DialogPropertyValueEditor
{
    public CDSceneSelectorEditor()
    {

        string template = 
            @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
//                          xmlns:pe='clr-namespace:System.Activities.Presentation.PropertyEditing;assembly=System.Activities.Presentation'>
                <DockPanel LastChildFill='True'>
                <pe:EditModeSwitchButton TargetEditMode='Dialog' Name='EditButton' 
                                         DockPanel.Dock='Right' Content='...' />
                <TextBlock Text='Picture' Margin='2,0,0,0' 
                           VerticalAlignment='Center'/>
            </DockPanel>
        </DataTemplate>";

        try
        {
            using (var sr = new MemoryStream(Encoding.UTF8.GetBytes(template)))
            {
                this.InlineEditorTemplate = (DataTemplate)(XamlReader.Load(sr));
            }
        }
        catch (Exception ex)
        {
            if (ex != null) {}
        }
    }
}

So, I figured that the visual tree must be the issue, so I tried it this way, which gave me a VistalTree, but didn't change the result:
C#
public class CDSceneSelectorEditor : DialogPropertyValueEditor
{
    public CDSceneSelectorEditor()
    {
        var button = new FrameworkElementFactory(typeof(EditModeSwitchButton))
                         { Name = "EditButton"     };
        button.SetValue(DockPanel.DockProperty, Dock.Right);
        button.SetValue(EditModeSwitchButton.TargetEditModeProperty, 
                        PropertyContainerEditMode.Dialog);
        button.SetValue(EditModeSwitchButton.ContentProperty, " ... ");
        button.SetValue(TextBlock.MarginProperty, new Thickness(0,0,3,0));

        var image  = new FrameworkElementFactory(typeof(Image))
                         { Name = "ThumbnailImage" };
        image.SetValue(Image.StretchProperty, 
                       System.Windows.Media.Stretch.Uniform);

        var panel  = new FrameworkElementFactory(typeof(DockPanel));
        panel.AppendChild(button);
        panel.AppendChild(image);

        DataTemplate template = new DataTemplate();
        template.VisualTree = panel;
        this.InlineEditorTemplate = template;
    }
}


I am out of ideas, and google has been no help at all.
Tags: C#, WPF, PropertyGrid, Extended.Toolkit

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900