开发者

displaying a list of custom objects as a property of an object bount to .SelectedObject in PropertyGrid

I'm going to take another crack at this question b/c the original question I posted earlier was poorly formed, and I did not do enough research first to present a clear, concise question regarding the PropertyGrid. Hopefully, this will be better.

I want to bind this object to the Property Grid:

public class Analytic
{
    public enum Period { Daily, Monthly, Quarterly, Yearly};
    publi开发者_运维问答c Analytic()
    {
        this.Benchmark = new List<Benchmark>();
    }
    public List<Benchmark> Benchmark { get; set; }
    public Period Periods { get; set; }
    public void AddBenchmark(Benchmark benchmark)
    {
        if (!this.Benchmark.Contains(benchmark))
        {
            this.Benchmark.Add(benchmark);
        }
    }
}

The object exposes two types of properties, one, an enum type, and two, a custom type of type Benchmark. I want both types to be displayed as drop-down lists. I know that PropertyGrid automatically creates a drop-down list for the enum-based property. My problem is trying to get the list of Benchmarks to display as a drop-down list. What I would like is to have the Name property of Benchmark to be the text that appears in the drop-down list. Here is the code for Benchmark:

public class Benchmark
{
    public Benchmark(string id, string name)
    {
        this.ID = id;
        this.Name = name;
    }
    public string ID { get; set; }
    public string Name { get; set; }
}

Trying to bind the object to a PropertyGrid, using this calling code:

Analytic analytic = new Analytic();
analytic.AddBenchmark(new Benchmark("1", "BM1"));
analytic.AddBenchmark(new Benchmark("2", "BM2"));
propertyGrid1.SelectedObject = analytic;

results in this output:

displaying a list of custom objects as a property of an object bount to .SelectedObject in PropertyGrid

in the screen-grab above, you can see the list of Benchmark objects is being rendered as "(Collection)". I want the names of each Benchmark to appear just how the Periods enum appears. The output would be something like this:

displaying a list of custom objects as a property of an object bount to .SelectedObject in PropertyGrid

The big question is, how to do this?

I've seen a couple examples of "helper" classes inherit from "System.ComponentModel.StringConverter", but this usually assumes the collection itself is an array or a collection of Strings. There are good examples of using StringConverter HERE (build a drop-down list of States) and HERE (builds a drop-down list of Rules).

In my example, my collection is a collection of a custom type, Benchmark, and I still don't know:

  1. What, if any base classes/interfaces I can use that will help me do what I need to do for Benchmark, similar to how the above two links helped for String types.
  2. If there is one good, simple example anywhere online of what I need to accomplish

I find it insane that such a trivial task of showing a drop-down list of non-primitive types on some exposed properties of an object bound to the PropertyGrid is so painful.

Again, any help, or any pointers in the right direction would be much appreciated.

Thanks, Mike


I know this is old, but maybe it will help someone else.

Seems to me you can decompose this problem into 2 parts: first, you want to display a custom list of strings in the propertygrid. 2nd, you want to set a property in the custom object, based on one selection from that custom list of strings. Breaking it down this way, it gets pretty easy. For the first part, you can use a TypeConverter.

[TypeConverter(typeof(MyCustomSelect))]
public String Flavor { get; set; }

The MyCustomSelect is a type that simply provides a list of strings to select from. For example:

public class MyCustomSelect : TypeConverter
{
    public override bool
    GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true; // display drop
    }
    public override bool
    GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true; // drop-down vs combo
    }
    public override StandardValuesCollection
    GetStandardValues(ITypeDescriptorContext context)
    {
        // can look at context to build list, if necessary
        return new StandardValuesCollection(new string[] { "abc", "def", "ghi" });
    }
}

Ok, now, this is a string property. It's not that custom type you wanted. How do you set a property on your object, when a new value is selected for this string? Just implement a setter.

    [TypeConverter(typeof(MyCustomSelect))]
    public String Flavor
    {
        get { return _flavor; }
        set 
        {
            _flavor = value;
            OtherProperty = OtherCustomObject.Create(_flavor);
        }
    }
    private string _flavor;

    [Browsable(false)]
    public OtherCustomObject OtherProperty { get; set; }

The factory method OtherCustomObject.Create() is left for you to implement.


Now, if you really want to set a List of other custom objects, you need to implement a collection editor, and within THAT, you can display the dropdown.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜