We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Conditional ReadOnly property

I have 2 properties that are dependent - Marital Staus and Spouse Name. Spouse Name should be editable (ReadOnly = False) when Marital Status is checked. I have tried the following but is not working.


public class Employee
{
    string name;
    bool isMarried;
    string spouseName;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    [System.ComponentModel.RefreshProperties(RefreshProperties.All)]
    public bool IsMarried
    {
        get { return isMarried; }
        set
        {
            isMarried = value;
            bool newValue = !value;
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["SpouseName"];
            ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
            FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
            isReadOnly.SetValue(attrib, newValue);
        }
    }

    [ReadOnly(true)]
    public string SpouseName
    {
        get { return spouseName; }
        set
        {
            spouseName = value;
        }
    }
}

2 Replies

UN Unknown Syncfusion Team November 11, 2019 12:32 PM UTC

Hi Satya Swarup, 

Thank you for patience. 

Attributes are defined at compile time, it cannot be changed at runtime, it is not possible to use attributes for runtime changes. Currently we are trying to find an alternative solution for this query. We will update you regarding this query on Nov 13, 2019. 

Regards, 

Niranjan Kumar 



KP Kanniyappan Panneer Selvam Syncfusion Team November 13, 2019 10:29 AM UTC

Hi Satya Swarup,  
 
Thanks for your patience. 
 
As per current implementation of PropertyGrid, we can change the properties of PropertyItem when adding the property items to PropertyGrid through AutoGeneratingPropertyGridItem event. But, PropertyGrid does not have direct support to change property as read-only during runtime. So, we have prepared a workaround sample based on your requirement and it can be achieved by using the same event by refreshing the PropertyGrid, once the property [IsMarried] value is changed, this event will be triggered. Please find the code snippet, sample for the same. 
 
Code Snippet: 
 
private void Custom_AutoGeneratingPropertyGridItem(object sender, AutoGeneratingPropertyGridItemEventArgs e) 
        { 
            if (e.DisplayName == "SpouseName") 
            { 
                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(Custom.SelectedObject.GetType())["IsMarried"]; 
                object value = descriptor.GetValue(Custom.SelectedObject); 
                e.ReadOnly = !((bool)value); 
            } 
        } 
 
public bool IsMarried 
{ 
    get { return isMarried; } 
    set 
        { 
          isMarried = value; 
          PropertyGrid.RefreshPropertygrid(); 
        } 
} 
 
 
 
Please try the above solution and let us know if it is helpful. 
 
Regards, 
Kanniyappan P 


Loader.
Up arrow icon