Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Readonly property on PropertyGrid

Readonly property on PropertyGrid

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luis Barreira
    wrote on last edited by
    #1

    I hava a situation where I have a class that contains objects os other classes that are to be readonly (the whole thing is initialized in the constructor, and the gris is used only to show the results). The problem is that the TypeConverters for some of the inner objects are allowing to edit the object: using System; using System.ComponentModel; namespace PropertyTest { public class SimpleClass { private NestedClass _xy; private int _z; public NestedClass XY { get { return _xy; } } public int Z { get { return _z; } } public SimpleClass() { _xy = new NestedClass(1, 2); _z = 3; } } [TypeConverter(typeof(NestedClassConverter))] public class NestedClass { private int _x; private int _y; public int X { get { return _x; } set { _x = value; } } public int Y { get { return _y; } set { _y = value; } } public NestedClass(int x, int y) { _x = x; _y = y; } } public class NestedClassConverter : ExpandableObjectConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(NestedClass)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(System.String) && value is NestedClass) { NestedClass nc = (NestedClass)value; return nc.X.ToString() + "," + nc.Y.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } } } If I create an object of type SimpleClass, I want the property of type NestedClass to be readonly on the propertygrid. How can I do that? Luis Barreira

    L 1 Reply Last reply
    0
    • L Luis Barreira

      I hava a situation where I have a class that contains objects os other classes that are to be readonly (the whole thing is initialized in the constructor, and the gris is used only to show the results). The problem is that the TypeConverters for some of the inner objects are allowing to edit the object: using System; using System.ComponentModel; namespace PropertyTest { public class SimpleClass { private NestedClass _xy; private int _z; public NestedClass XY { get { return _xy; } } public int Z { get { return _z; } } public SimpleClass() { _xy = new NestedClass(1, 2); _z = 3; } } [TypeConverter(typeof(NestedClassConverter))] public class NestedClass { private int _x; private int _y; public int X { get { return _x; } set { _x = value; } } public int Y { get { return _y; } set { _y = value; } } public NestedClass(int x, int y) { _x = x; _y = y; } } public class NestedClassConverter : ExpandableObjectConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(NestedClass)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(System.String) && value is NestedClass) { NestedClass nc = (NestedClass)value; return nc.X.ToString() + "," + nc.Y.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } } } If I create an object of type SimpleClass, I want the property of type NestedClass to be readonly on the propertygrid. How can I do that? Luis Barreira

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      [ReadOnly]
      public NestedClass XY
      {
      get { return _xy; }
      }

      xacc.ide-0.1.1.10 - now with AutoComplete(kinda) :)

      L 1 Reply Last reply
      0
      • L leppie

        [ReadOnly]
        public NestedClass XY
        {
        get { return _xy; }
        }

        xacc.ide-0.1.1.10 - now with AutoComplete(kinda) :)

        L Offline
        L Offline
        Luis Barreira
        wrote on last edited by
        #3

        I've tried that, and I also tried declaring the variable as private readonly NestedClass _xy; but it still let's me edit XY's properties...

        L 1 Reply Last reply
        0
        • L Luis Barreira

          I've tried that, and I also tried declaring the variable as private readonly NestedClass _xy; but it still let's me edit XY's properties...

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Maybe apply the typeconvertor to the property instead of the type, and the ReadOnly attribute.

          xacc.ide-0.1.1.10 - now with AutoComplete(kinda)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups