Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
I am trying to create a user control with custom properties. I have a property named Server.

And Values for this property is system dependent. I have the code that can generate the values and is working fine.

During the design time, when I include this control in my application, and type the property name, I want Visual Studio to prompt for the values. Like it does for Alignment property for some controls shows Left, Right, Center.. during design time.

If the Values are static, I can use an enum and it works fine. I need help with dynamic values
Posted

1 solution

Before re-posting the question (or posting very much related question), you should have seriously think at my answers you already got:
How to get response when click PropertyGrid,
How to show values for custom property during desgin-time C# WPF?.

In addition to that: as you want to "add the properties" at design time, they should not be the "real" properties. You have to model properties (based, for example, on dictionaries, as I suggested in the second answer referenced above) and develop classes modeling objects with properties. In other word, if you add such "property" not to the class (what would it possibly mean?) but to the instance of the class "modeling properties behavior".

As you want to do this all in design time as well, it leads to the idea that you should use PropertyGrid with the wrapper class representing the instance of the "modeling class" described above; this class needs to implement the interface System.ComponentModel.ICustomTypeDescriptor. "Properties" manipulation (that is, modification of data store in the dictionary), will need implementation of specialized editor embedded in property grid. How?
Note that this interface will allow you to implement object which composes other objects also implementing this interface. And this interface has the method System.ComponentModel.ICustomTypeDescriptor.GetEditor you will have to implement in specialized way, to express the process of adding/removing "properties".

I understand that it cannot be made clear at once. You need to read the answers, which a pretty big, and the documentation thoroughly, and check up your knowledge of the fundamentals. If something was confusing, you could ask follow-up questions. It all is really pretty hard to comprehend at once.

It is possible that your re-post is related to some confusion and underestimation of this pretty serious and labor-taking problem. Do you think there is a simpler "magical" way? I don't thing so. Prepare for the serious work.

[EDIT]

Now, some sample code for you, just to give you and idea how you can model "properties" of different type using the same class (in my example) instance, with variable set of properties:
C#
using IntPropertyDictionary = System.Collections.Generic.Dictionary<string, int>;
using StringPropertyDictionary = System.Collections.Generic.Dictionary<string, string>;
using DoublePropertyDictionary = System.Collections.Generic.Dictionary<string, double>;
using PropertyNameList = System.Collections.Generic.List<string>;
// anything else, including your custom types for value types...

public class ObjectWithModeledProperties {

    public void Add(string key, int value) {
        intPropertyNameList.Add(key);
        intPropertyDictionary.Add(key, value);
    }
    // and so on, with other types...
    // then implement insert/remove, etc.,
    // keep lists and dictionaries in sync

    // int:
    public void SetPropertyValue(int index, int value) {
        string key = intPropertyNameList[index];
        this.intPropertyDictionary[key] = value;
    }
    public int GetIntPropertyValue(int index) {
        string key = intPropertyNameList[index];
        return this.intPropertyDictionary[key];
    }
    public void SetPropertyValue(string key, int value) {
        this.intPropertyDictionary[key] = value;
    }
    public int GetIntPropertyValue(string key) {
        return this.intPropertyDictionary[key];
    }

    // string:
    public void SetPropertyValue(int index, string value) {
        string key = stringPropertyNameList[index];
        this.stringPropertyDictionary[key] = value;
    }
    public string GetStringPropertyValue(int index) {
        string key = stringPropertyNameList[index];
        return this.stringPropertyDictionary[key];
    }
    public void SetPropertyValue(string key, string value) {
        this.stringPropertyDictionary[key] = value;
    }
    public string GetStringPropertyValue(string key) {
        return this.stringPropertyDictionary[key];
    }

    // double:
    // same thing...
    // and so on...

    IntPropertyDictionary intPropertyDictionary =
        new IntPropertyDictionary();
    StringPropertyDictionary stringPropertyDictionary =
        new StringPropertyDictionary();
    DoublePropertyDictionary doublePropertyDictionary =
        new DoublePropertyDictionary();
    PropertyNameList intPropertyNameList = new PropertyNameList();
    PropertyNameList stringPropertyNameList = new PropertyNameList();
    PropertyNameList doublePropertyNameList = new PropertyNameList();
    //... 

} //class ObjectWithModeledProperties

As you can see, you will be able to access all properties by either string key or by index. Normally, you don't know they keys (hard-coding would be bad), but integrated in the UI, with access by index, you will always use the indices found from some UI element or control (such as list box), where the indices correspond to property indices by design. Again, these lists and dictionaries will throw exception if you make a bug (do yourself great favor, don't handle exceptions locally in this class code, let go).

Not that all different SetPropertyValue method have the same name, for different type and different access (key or index).

Implementation could be better, more elegant and efficient; I put it just to give an idea on some simplest implementation example. You can also use any custom properties types, anything. Only the number of types should be fixed. (With polymorphic dictionaries, it would allow unlimited extensions in OOP style.)

I cannot give you code for customization of GridView to implement the edition of the property set (in design mode, too), not only values, because it would take whole article, quite big one and pretty complicated. I think I gave you all the ideas in the referenced articles. Feel free to ask your questions.


Congratulations with 1st of April! :-)

I would like to use the occasion to invite everyone to see my new 1 of April publication and have some fun:

Some Programming Approaches to "Neuro-Linguistic Programming".
Participation in this game in Comments and Discussions is especially encouraged.

Thank you.
—SA
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900