Re: iteration (property grid)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Gravell

    Re: iteration (property grid)

    Well, what exactly do you want to do?

    PropertyGrid is really at the whim of System.Componen tModel - and
    there are various ways of investigating properties; the most common
    being TypeDescriptor. GetProperties() . The tricky bit is sub-
    properties, since to walk everything you'd need to look at
    TypeConverters - it wouldn't be much fun.

    Did you see the PropertyGrid/error-provider example I did for you the
    other day? This might be an option. Alternatively you can provide your
    own custom tab that could make this data more public (since it is tabs
    that are asked to provide properties).

    Alternatively, you might use IDataErrorInfo directly, since this is
    the same validation that drives (as one example) the validation in
    DataGridView.

    So; what exactly do you want to do?

    Marc
  • csharpula csharp

    #2
    Re: iteration (property grid)

    Hello,
    I have only one level of properties field ,don't have sub fields. So is
    there a good solution for that case? All I need it to iterate through
    them and to check if the values are valid.
    Thanks again!


    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Marc Gravell

      #3
      Re: iteration (property grid)

      Well, there are various ways of enumerating the properties, but I'm not
      sure how that helps you validate things... you are going to have to find
      some fun ways of validating arbitrary properties. That is why I
      suggested IDataErrorInfo, as this provides a supported mechanism for an
      object to validate *itself* and report any inconsistencies .

      For enumerating the properties of an object, the simplest approach is
      something like:

      foreach(Propert yDescriptor prop in TypeDescriptor. GetProperties(o bj))
      {
      Console.WriteLi ne("{0}={1}", prop.Name, prop.GetValue(o bj));
      }

      However, note that PropertyGrid is a highly customisable control. For
      example, it applies attribute-based filtering to properties (defaults to
      [Browsable(true)], and allows individual tabs to shim other members as
      properties - for example the "events" tab in VS.
      Thus, if you have a complex setup, then to get the same list of
      properties that PropertyGrid is using, you would have to do something like:

      Attribute[] attribs =
      new Attribute[grid.BrowsableA ttributes.Count];
      grid.BrowsableA ttributes.CopyT o(attribs, 0);

      foreach (PropertyDescri ptor prop in
      grid.SelectedTa b.GetProperties (grid.SelectedO bject, attribs))
      {
      Console.WriteLi ne("{0}={1}",
      prop.Name, prop.GetValue(g rid.SelectedObj ect));
      }

      But again - unless you have some metadata-based idea for validing
      individual properties, I'm not sure how this is going to help you...

      Marc

      Comment

      • csharpula csharp

        #4
        Re: iteration (property grid)



        but in the following code :
        Console.WriteLi ne("{0}={1}",
        prop.Name, prop.GetValue(g rid.SelectedObj ect));


        I don't have grid.SelectedOb ject ,I want I am doing it this way :

        (prop.DisplayNa me,prop.Value?? ) How can I get a value associated with
        current property in the iteration ? I can't do it with SelectedObject
        because i am going over the values on load and not on click.
        Thank u!

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Marc Gravell

          #5
          Re: iteration (property grid)

          The SelectedObject is, confusingly, nothing to do with a selection; it
          simply holds the object that we want to display properties for. You have
          to have an object to use PropertyGrid...

          (not to be confused with SelectedGridIte m, which *is* related to a
          regular selection).

          Marc

          Comment

          Working...