Articles in this section
Category / Section

How to Change the ReadOnly of Specific Property Values at Run Time in PropertyGrid

3 mins read

This article demonstrates how to change the ReadOnly of a specific property values within the Syncfusion PropertyGrid control at run time. You can utilize the ValueChanged and AutoGeneratingPropertyGridItem events to achieve this functionality.

Step 1 : Create a Model and ViewModel classes with the properties for Email_ID, Name, Age, Experience, and ReadOnly.

C#

public class Model
{
   public string Email_ID { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
   public int Experience { get; set; }
   public bool ReadOnly { get; set; }
}
public class ViewModel
{
   public object SelectedEmployee { get; set; }
   public ViewModel()
   {
       SelectedEmployee = new Model()
       {
           Age = 25,
           Name = "mark",
           Experience = 5,
           Email_ID = "mark@gt",
           ReadOnly = false,
       };
   }
}

Step 2: Set the properties as read-only or editable using the ReadOnly or Editable attributes during load time in the PropertyGrid. When a property is marked as ReadOnly=true or Editable=false, the PropertyGrid will prevent you from editing the property values. For example, you can set the Age property as read-only (ReadOnly=true) and the Experience property as editable (Editable=false).

XAML

public class Model
{
   public string Email_ID { get; set; }
   public string Name { get; set; }
   [ReadOnly(true)]
   public int Age { get; set; }
   [Editable(false)]
   public int Experience { get; set; }
   public bool ReadOnly { get; set; }
}

Step 3: Change the properties as ReadOnly at run time by using the event handlers ValueChanged and AutoGeneratingPropertyGridItem of PropertyGrid.

C#

<syncfusion:propertygrid x:name="propertyGrid" selectedobject="{Binding SelectedEmployee}" valuechanged="PropertyGrid_ValueChanged" autogeneratingpropertygriditem="PropertyGrid_AutoGeneratingPropertyGridItem">
   <syncfusion:propertygrid.datacontext>
       <local:viewmodel></local:viewmodel>
   </syncfusion:propertygrid.datacontext>
</syncfusion:propertygrid>

Step 4: Implement the PropertyGrid_ValueChanged event handler to respond to changes in the ReadOnly property. Additionally, ensure to refresh the PropertyGrid whenever the ReadOnly property changes. The provided code sample demonstrates how to dynamically update the ReadOnly value for the specific property and reflect these changes in the PropertyGrid control.

C#

//Inside the MainWindow class, declare a boolean field named IsChecked with an initial value of false.
public bool IsChecked = false;

//In the PropertyGrid_ValueChanged event handler, update the IsChecked field value based on the ReadOnly property of the selected Employee object.
private void PropertyGrid_ValueChanged(object sender,ValueChangedEventArgs args)
{
   var propertyGrid = sender as PropertyGrid;
   var selectedobj = propertyGrid.SelectedObject as Model;
   if (args.PropertyItem.Name == "Readonly")
   {          
       this.IsChecked = selectedobj.Readonly ? true : false;
       propertyGrid.RefreshPropertygrid();
   }        
}

Step 5: In the PropertyGrid_AutoGeneratingPropertyGridItem event handler, the ‘Age’ property’s read-only status is determined by the IsChecked Boolean field. IsChecked is true, ‘Age’ becomes read-only and IsChecked is false, ‘Age’ remains editable.

private void PropertyGrid_AutoGeneratingPropertyGridItem(object sender, AutoGeneratingPropertyGridItemEventArgs e)
{
   if (e.DisplayName == "Age")
             e.ReadOnly = IsChecked;
}

Output:

ezgif.com-video-to-gif (10).gif

Figure:1 Checked and Unchecked state of ReadOnly

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied