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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Conditional ReadOnly property

Thread ID:

Created:

Updated:

Platform:

Replies:

148932 Nov 8,2019 04:18 AM UTC Nov 13,2019 10:29 AM UTC WPF 2
loading
Tags: PropertyGrid
ssm
Asked On November 8, 2019 04:18 AM UTC

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;
        }
    }
}

Niranjan Kumar Gopalan [Syncfusion]
Replied On 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 


Kanniyappan Panneer Selvam [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon