This project has moved and is read-only. For the latest updates, please go here.

PropertyGrid: custom editor propertyItem.Instance is null

Sep 24, 2014 at 5:50 PM
Edited Sep 24, 2014 at 5:53 PM
Hi,

I have created my own customer editor. In the ResolveEditor(...) method, I tried to access propertyItem.Instance but it always give me null.

I am using PropertyGrid plus editor v2.3.14317.10540.

Would you please advise how to fix this problem?

Thanks.
Sep 24, 2014 at 9:23 PM
Hi,

Can you give a code snippet to be able to reproduce the issue ?
Thanks.
Sep 24, 2014 at 10:08 PM
The customer editor code is as follows:
public class DefaultUomEditor : ITypeEditor
{
    private TextBox _tb;

    public DefaultUomEditor()
    {
        _tb = new TextBox();
    }

    public System.Windows.FrameworkElement ResolveEditor(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
    {
        propertyItem.IsReadOnly = true;
        //create the binding from the bound property item to the editor
        var _binding = new Binding("Value"); //bind to the Value property of the PropertyItem
        _binding.Source = propertyItem;
        _binding.ValidatesOnExceptions = true;
        _binding.ValidatesOnDataErrors = true;
        _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
        BindingOperations.SetBinding(_tb, TextBox.TextProperty, _binding);

        if (propertyItem.Instance != null)
        {
            // do something...
        }

        return _tb;
    }

}
And the PropertyGrid.SelectedObject is bound to an object instance which has the following property defined as:
    [DisplayName("Default UOM")]
    [Editor(typeof(DefaultUomEditor), typeof(DefaultUomEditor))]        
    public string Uom
    {
        get { return this._uom; }
        internal set
        {
            this._uom = value;
            RaisePropertyChanged(() => this.Uom);
        }
    }
Sep 25, 2014 at 1:53 PM
Hi, using your code in a small stand-alone application, I place a breakpoint in DefaultUomEditor.ResolveEditor() and propertyItem.Instance is not null. Could it be related to something else ?

Also, I suggest using public setter instead of internal setter for property "Uom" if you want to use a specific editor. An editor should be used when a property can be edited.