Articles in this section
Category / Section

How to restrict editing of Properties in PropertyGrid?

2 mins read

In order not to restrict the editing of the values in PropertyGrid, the ReadOnly attribute would be used. The same has been explained in the following code:

XAML:

 
<Window x:Class="_186824PropertyGridsample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_186824PropertyGridsample"
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <syncfusion:PropertyGrid x:Name="_Propertygrid"></syncfusion:PropertyGrid>
    </Grid>
</Window>
 

 

C#

 
    namespace PropertyGridSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
            propertygrid.SelectedObject = new Person();
 
 
        }
      
    }
 
 
 
    public class Bank
    {
        [DisplayNameAttribute("Name")]
        [DescriptionAttribute("Name of the bank.")]
        public string Name
        {
            get;
 
            set;
        }
 
        [DisplayNameAttribute("Customer ID")]
        [DescriptionAttribute("Customer ID used for Net Banking.")]
        [ReadOnly(true)]
        public string CustomerID
        {
            get;
 
            set;
        }
 
        [DescriptionAttribute("Password used for Net Banking.")]
        public string Password
        {
            get;
 
            set;
        }
 
        [DisplayNameAttribute("Account Number")]
        [DescriptionAttribute("Bank Account Number.")]
        public long AccountNumber
        {
            get;
 
            set;
        }
 
 
        public override string ToString()
        {
            return Name;
        }
 
    }
 
    public enum Gender
    {
        Male,
 
        Female
    }
 
    public enum Country
    {
        UnitedStates,
 
        Germany,
 
        Canada,
 
        Mexico,
 
        Alaska,
 
        Japan,
 
        China,
 
        Peru,
 
        Chile,
 
        Argentina,
 
        Brazil
    }
 
 
    public class Person
    {
        public Person()
        {
            FirstName = "Carl";
            LastName = "Johnson";
            Age = 30;
            Mobile = 91983467382;
            Email = "carljohnson@gta.com";
            ID = "0005A";
            FavoriteColor = Brushes.Gray;
            IsLicensed = true;
            DOB = new DateTime(1987, 10, 16);
            Key = "dasd798@79hiujodsa';psdoiu9*(Uj0JK)(";
            Bank = new Bank() { Name = "Centura Banks", AccountNumber = 00987453721, CustomerID = "carljohnson", Password = "nuttertools" };
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("First Name")]
        [DescriptionAttribute("First Name of the actual person.")]
        public string FirstName
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("Last Name")]
        [DescriptionAttribute("Last Name of the actual person.")]
        public string LastName
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("ID")]
        [DescriptionAttribute("ID of the actual person.")]
        public string ID
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("Date of Birth")]
        [DescriptionAttribute("Birth date of the actual person.")]
        public DateTime DOB
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Contact Details")]
        [DisplayNameAttribute("Email ID")]
        [DescriptionAttribute("Email address of the actual person.")]
        public string Email
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Contact Details")]
        [DisplayNameAttribute("Mobile Number")]
        [DescriptionAttribute("Contact Number of the actual person.")]
        public long Mobile
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("Age")]
        [DescriptionAttribute("Age of the actual person.")]
        public int Age
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("Image")]
        [DescriptionAttribute("Photo shot of the actual person.")]
        public ImageSource Image
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Favorites")]
        [DisplayNameAttribute("Favorite Color")]
        [DescriptionAttribute("Favorite color of the actual person.")]
        public Brush FavoriteColor
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Identity")]
        [DisplayNameAttribute("Gender")]
        [DescriptionAttribute("Gender information of the actual person.")]
        public Gender Gender
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Location")]
        [DisplayNameAttribute("Country")]
        [DescriptionAttribute("Country where the actual person locating.")]
        public Country Country
        {
            get;
 
            set;
        }
 
        [Browsable(false)]
        public string MaritalStatus
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("License Details")]
        [DisplayNameAttribute("IsLicensed")]
        [DescriptionAttribute("Determines whether the person is licensed or not.")]
        public bool IsLicensed
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("License Details")]
        [DisplayNameAttribute("Key")]
        [DescriptionAttribute("License key value of the person.")]
        public string Key
        {
            get;
 
            set;
        }
 
        [CategoryAttribute("Account Details")]
        [DisplayNameAttribute("Bank")]
        [DescriptionAttribute("Bank in which he has account.")]
        //It shows how to set the field as ReadOnly
        [ReadOnly(true)]
        public Bank Bank
        {
            get;
 
            set;
        }
    }
 
    }
 

 

It shows the Bank field is readonly property

                                         Fig i: It shows the Bank field is readonly property.

 

Sample Links:

How to restrict editing of Properties in PropertyGrid C#?

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