如何在设计时动态地为自定义属性添加值? [英] How to add values dynamically for custom property during design time?

查看:141
本文介绍了如何在设计时动态地为自定义属性添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义属性创建用户控件。我有一个名为Server的属性。



此属性的值取决于系统。我有代码可以生成值并且工作正常。



在设计期间,当我在我的应用程序中包含此控件时,键入属性名称,我希望Visual Studio提示输入值。就像它对于某些控件的Alignment属性一样,在设计时显示Left,Right,Center ..



如果值是静态的,我可以使用枚举它工作良好。我需要动态值的帮助

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

推荐答案

在重新发布问题(或发布非常相关的问题)之前,你应该认真考虑我已经得到的答案:

点击时如何获得回复PropertyGrid

如何在设计时间C#WPF期间显示自定义属性的值?



除此之外:因为你想添加属性在设计时,它们不应该是真正的属性。您必须建模属性(例如,基于字典,正如我在上面引用的第二个答案中所建议的那样)并开发具有属性的对象建模类。换句话说,如果你不在类中添加这样的属性(它可能意味着什么?),而是添加类建模属性行为的实例。



如果你想在设计时间内完成这一切,那么你会想到你应该使用 PropertyGrid ,其中包装类代表建模类的实例 如上所述;这个类需要实现接口 System.ComponentModel.ICustomTypeDescriptor 。 属性操作(即修改字典中的数据存储),需要实现嵌入在属性网格中的专用编辑器。如何?

请注意,此接口将允许您实现构成同样实现此接口的其他对象的对象。并且此接口具有方法 System.ComponentModel.ICustomTypeDescriptor.GetEditor 您必须以专门的方式实现,以表达添加/删除属性的过程。



我明白它不能立刻说清楚。您需要阅读相当大的答案和文档,并检查您对基础知识的了解。如果有些事情令人困惑,你可以提出后续问题。这一切都非常难以立即理解。



您的重新发布可能与某些混乱和低估这种非常严重和劳动力有关问题。你认为有一种更简单的神奇方式吗?我不是这样的。准备认真的工作。







现在,为您提供一些示例代码,给你并想法如何使用相同的类(在我的例子中)对具有可变属性集的实例建模不同类型的属性:

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.



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:
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



如您所见,您将能够通过字符串键或索引访问所有属性。通常情况下,你不知道它们的键(硬编码会很糟糕),但是在UI中集成,通过索引访问,你将始终使用从某些UI元素或控件(如列表框)中找到的索引,其中指数按设计对应物业指数。再次,如果你犯了一个错误,这些列表和词典会抛出异常(自己大忙,不要在这个类代码中本地处理异常,放手)。



并非所有不同的 SetPropertyValue 方法都具有相同的名称,用于不同的类型和不同的访问(键或索引)。



实施可以更好,更优雅,更有效率;我只想对一些最简单的实现示例给出一个想法。您还可以使用任何自定义属性类型。只应修复类型数量。 (使用多态字典,它将允许OOP样式的无限扩展。)



我无法为您提供自定义 GridView的代码实现属性集的版本(在设计模式中也是如此),不仅仅是值,因为它需要整篇文章,相当大而且相当复杂。我想我在参考文章中给了你所有的想法。随意提出您的问题。




祝贺4月1日! :-)



我想利用这个机会邀请大家看一下我4月份新发布的1并有一些乐趣:



某些编程方法神经语言程序设计

特别鼓励在评论和讨论中参与此游戏。



谢谢。

-SA


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


这篇关于如何在设计时动态地为自定义属性添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆