Control's property not updated properly when change in designer
Asked Answered
T

1

7

I have created a custom control and component as like the below code,

public class CustomComponent : Component
{
    private string style;
    public CustomControl Control { get; set; }
    public string Style
    {
        get
        {
            return style;
        }
        set
        {
            style = value;
            Control.Style = value;
        }
    }
}

public class CustomControl : Control
{
    string style;  
    public string Style
    {
        get
        {
            return style;
        }
        set
        {
            style = value;
        }
    }
}

After that i have added the control into the form and component into the form. And then try to assign the Component.Control value. After assign the value if i try to change the style property of component, the style property in control is not changed in designer level as like the below image,

Style not updated in control

If I have clicked on the Style property of the control its will get updated as like the below image,

enter image description here

Tumefacient answered 1/3, 2017 at 12:50 Comment(0)
A
7

You need to correct a couple of things in the code. The Style property of your CustomComponent should be changed to this:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
public string Style 
{
    get 
    {
        if (Control != null)
            return Control.Style;
        else
            return null;
    }
    set 
    {
        if (Control != null)
            Control.Style = value;
    }
}

You should check if the Control is not not, get or set the Style value of control. You don't need to define a member variable to store the property value while it belongs to another control.

Also since you don't need to serialize the property for your component (since it has been serialized for your control), so decorate it with DesignerSerializationVisibility attribute having Hidden value.

Also when you want to refresh the PropertyGrid to show changes in other property (like Control.Style property) when you edit Style property of your component, decorate it with RefreshProperties attribute having RefreshProperties.All value.

Aeneid answered 1/3, 2017 at 13:6 Comment(6)
Hi Reza, As per your suggestion i have modified the codes in my sample. But still the problem occurs. In control's Style property is getting updated, but problem is the the "Style" property of the control (which present inside the component) is not get updated. If i click over the style property of control(Inside the component) it's get updated. So i think the problem is refreshing the designer items. So can you please suggest any other way to solve this?Tumefacient
I got this part of your question now, I edited the answer and added RefreshProperties.Aeneid
I doubt this approach will work either. By default, a property change will the trigger the update of that particular property.Pointed
@ChibuezeOpata You can simply test it. Updating Style property of Component will not reflect in Style property of the Control in PropertyGrid. The scenario of test is this way: 1)Expand Control Property. 2) Set Style property of Component and the the result with and without RefreshProperties attribute.Aeneid
@RezaAghaei, Thanks for you answer, now its working fine. But i have another doubt, in component class instead of using string, i try to use the enum type for style property. When using enumeration(Style property of component), I select value(Style property in component) in designer level it is automatically updates the style property of control also without using any RefreshProperties. How the enum type properties alone working properly without using the RefreshProperties?Tumefacient
You're welcome. The behavior is not limited to enumerations. If the property editor is a modal dialog like Font, or a dropdown like enumerations the behavior is the same and after editing the property value, property grid will refresh. Using UITypeEditor with Modal or DropDown as edit style has the same behavior.Aeneid

© 2022 - 2024 — McMap. All rights reserved.