Alert messages popping up with some property types

Jan 8, 2010 at 2:15 PM

 

Hi!

 

I've created a custom class that has a dependency property with the type of Style. Now obviously this property is not settable over the SL property grid however it apperars on it. This would be fine... but every time the grid loads I get a popup saying "Object of type 'System.String' cannot be converted to type 'System.Windows.Controls.ControlTemplate'". I wanted to get rid of this error message (at least on startup) as it's really disturbing and for me it's also irrevelant - I know there are some properties that cannot be set, I'm using the property grid to set others.

So I've debugged the issue and here's the reason:

Those properties that the editor can't assign to any other category are assigned to a StringValueEditor.  In StringValueEditor ln 71 there's this code:

void Control_TextChanged(object sender, TextChangedEventArgs e)
{
    if (this.Property.CanWrite )
    {               
        this.Property.Value = txt.Text;
    }
}

The exception doesn't occur here yet, it occurs in PropertyItem, in the Value setter. The exception is caught at ln 144 and passed to OnValueError which then triggers the event ValueError which is registered to StringValueEditor's property_ValueError method. So what I did to eliminate these annoying error messages was change 

 

void property_ValueError(object sender, ExceptionEventArgs e)
{
	MessageBox.Show(e.EventException.Message);
}

to

 

void property_ValueError(object sender, ExceptionEventArgs e)
{
	//MessageBox.Show(e.EventException.Message);
}

Hope this helps someone. I would also be interested if there is some other way to achieve disabling these popup errors for setting composite object properties?

Cheers,

 Gergely

 

 

void Control_TextChanged(object sender, TextChangedEventArgs e)
{
            if (this.Property.CanWrite )
            {               
                this.Property.Value = txt.Text;
            }
}

 

Coordinator
Jan 9, 2010 at 3:21 PM

Hi Gergely,

The simple solution without having to modify the source is to put a ReadOnly attribute on your new Style property. That will cause the line that checks for CanWrite to fail and skip trying to alter the underlying property value. The long way would be to create a new custom editor that supports modifying Style/Templates, maybe with a popup TextBox for editing or something.

Hope that helps...