Property Grid

  • Thread starter Thread starter amir
  • Start date Start date
A

amir

Is there a way to write all the items of a Property Grid to a file without
having to list every item one by one to be written to the file?
 
What format do you want the data? Would binary/xml serialization be an
option? To mirror the way that PropertyGrid presents properties, you
would need to ask the TypeConverter for the properties (normally
TypeDescriptor.GetProperties is used, but not with PropertyGrid...):

foreach (PropertyDescriptor prop in
TypeDescriptor.GetConverter(obj).GetProperties(obj))
{
object val = prop.GetValue(obj);
string s = prop.Converter.ConvertToString(val);
// do something with s
}

(actually, to be thorough you would need to implement an
ITypeDescriptorContext, but that might be overkill)

Any use?

Marc
 
Oops; TypeConverter is *sub*properties, not the first-order
properties. My bad - should use TypeDescriptor after all...
I'll make it up to you ;-p Here's an example including an
ITypeDescriptorContext:

using System;
using System.ComponentModel;
using System.Windows.Forms;

class Program
{
static void Main()
{
Form obj = new Form(); // why not...
// set some props, just to see how they work
obj.Name = "Some Name";
obj.Text = "Some Text";
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(obj))
{
object val = prop.GetValue(obj);
string displayName = prop.DisplayName;
if(string.IsNullOrEmpty(displayName)) displayName =
prop.Name;
SimpleContext ctx = new SimpleContext(obj, prop);
bool dirty = prop.ShouldSerializeValue(obj);
char dirtyMarker = dirty ? '*' : ' ';
string sVal = prop.Converter.ConvertToString(ctx, val);
Console.WriteLine("{0} {1}={2}", dirtyMarker, displayName,
sVal);
}
}
}
class SimpleContext : ITypeDescriptorContext
{
private readonly object instance;
private readonly PropertyDescriptor property;
public SimpleContext(object instance, PropertyDescriptor property)
{
this.instance = instance;
this.property = property;
}
bool ITypeDescriptorContext.OnComponentChanging() { return true; }
void ITypeDescriptorContext.OnComponentChanged() { }
IContainer ITypeDescriptorContext.Container { get { return
null; } }
public object Instance { get { return instance; } }
public PropertyDescriptor PropertyDescriptor { get { return
property; } }
object IServiceProvider.GetService(Type type) { return null; }

}
 
Hi Mark,

Since you are making use of controls that require a message pump (eg Form),
I think you need to decorate the Main method with the [STA] attribute.. But
then again, I may be full of it.



[STA]

static void Main()
 
mmmmm. actually, since you are not using Application.Run() then there is no
message pump anyway right?.. never mind then... oooooops :)
 
mmmmm. actually, since you are not using Application.Run() then there is no
message pump anyway right?.. never mind then... oooooops :)

Exactly - Form was just chosen as something with more than a couple of
properties, that would be instantly recognisable... it isn't actually
used, so no STA required ;-p

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top