BooleanConverter

Grids for WPF Forum

Posted 7 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Version: 12.1.0561
Environment: Windows 8 (64-bit)
Avatar

Hi,

I have a property with a custom BooleanConverter to display custom strings instead of "true" and 'false"

Here is the property:

[TypeConverter(typeof(UnitConverter))]
public bool unit
{
    get { return m_millimeterUnit; }
    set { m_millimeterUnit = value; }
}

 

And the converter:

 [ComVisible(false)]
    public class UnitConverter : BooleanConverter
    {
        public override object ConvertTo(ITypeDescriptorContext x_context, CultureInfo x_culture, object x_value, Type x_destinationType)
        {
            object l_ret;
            if ((typeof(string) == x_destinationType) && (x_value != null))
            {
                bool l_millimeterUnit = (bool)x_value;
                l_ret = l_millimeterUnit ? "mm":"Inches");
            }
            else
            {
                l_ret = base.ConvertTo(x_context, x_culture, x_value, x_destinationType);
            }
            return l_ret;
        }

        public override bool CanConvertTo(ITypeDescriptorContext x_context, Type x_destinationType)
        {
            return (typeof(string) == x_destinationType) || base.CanConvertTo(x_context, x_destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext x_context, CultureInfo x_culture, object x_value)
        {
            object l_ret;
            string l_value = x_value as string;
            if (l_value != null)
            {
                l_ret = (l_value !=  "Inches");
            }
            else
            {
                l_ret = base.ConvertFrom(x_context, x_culture, x_value);
            }
            return l_ret;
        }
    }

 The problem is that the standard propertygrid is working as expected, but not the Actipro's propertygrid which shows a checkbox instead.

How can I customize this checkbox and get a combobox with my 2 strings ("inches" and "mm") ?

Best regards,

Comments (5)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Stéphane,

A good source for more information on this is in the documentation under Topics>Actipro PropertyGrid>User Interface Layer>Property Editors. By default boolean values get a checkbox, but you can force a combobox by using a ComboBoxPropertyEditor. There are several ways to assign the editor, all of which are described in the documentation topic.


Actipro Software Support

Posted 7 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Avatar

Hi,

Ok I wrote this:

[TypeConverter(typeof(UnitConverter))]
[Editor(typeof(ActiproSoftware.Windows.Controls.PropertyGrid.Editors.ComboBoxPropertyEditor), typeof(ActiproSoftware.Windows.Controls.PropertyGrid.Editors.PropertyEditor))]
public bool unit
{
    get { return m_millimeterUnit; }
    set { m_millimeterUnit = value; }
}

 It's almost what I want.

The combo box shows the 2 possibilities: "mm" and "inches", but the initial value is not displayed (the cell is empty).

What did I miss ?

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Stéphane,

Could you send us a simple sample project containing the issue so that we can debug it and find what is going wrong? You may send the file to our support address. Please rename any .zip files to avoid them being blocked as spam, thank you.

[Modified 7 years ago]


Actipro Software Support

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Stéphane,

Thanks for the sample. Overall it might make much more sense in this case to use an enumeration instead, don't you think?

But if you do wish to use the boolean and a type converter, you'd want to change the Editor attribute to use ComboBoxStringPropertyEditor instead of ComboBoxPropertyEditor. The reason is that ComboBoxPropertyEditor binds directly to the bool value, which doesn't equal any of the standard value strings you defined (because they are strings). However ComboBoxStringPropertyEditor will call a custom type converter to convert the bool value to a string and use that as its selected item. So it does find matches in the ComboBox and your default value will show properly.


Actipro Software Support

Posted 7 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Avatar

Hi,

great !! Thank you for your support.

The latest build of this product (v2018.1 build 0675) was released 10 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.