如何在设计时C#WPF期间显示自定义属性的值? [英] How to show values for custom property during desgin-time C# WPF?

查看:250
本文介绍了如何在设计时C#WPF期间显示自定义属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF C#的新手,我正在尝试使用自定义属性创建用户控件。我有一个名为Planet的房产。



这个属性可以取值为水星,金星,地球和火星。



在设计期间,当我在我的应用程序中包含此控件并输入属性名称时,我希望Visual Studio提示这4个值。就像我们使用Alignment属性进行某些控制一样,它显示了Left,Right,Center ..



任何人都可以建议我如何做到这一点?

解决方案

会员10794476问:



我能够这是枚举,它是静态的。如果我想在运行时以编程方式生成属性值,那么最好的方法是什么呢?

这里有两个不同的问题,第二个问题首先取决于第一个,但第一个与第二个无关。



第一个是关于动态属性。粗略地说,这个主题也可以细分为两个。 One-A主题 是:您可以使用 System.Collections.Emit 动态生成代码。但只有在生成整个装配时才有可能。不存在汇编的方法也存在: DynamicMethod ,这与属性基本无关。因此,您可以生成整个程序集,您可以想象的所有类,结构和所有属性。但有什么用呢?如果它们是动态的,如何使用这些属性?您希望有任何预定义的编译时接口来处理动态生成的代码。考虑一下。通过属性名称或一些描述符(名称+属性类型)?可能,特别是如果属性类型集是编译时实体。但是值得努力吗?



我会说不会,因为,如果你创建这样一个访问属性的机器,它将超过使用生成的程序集(积极使用,但是为了实现纯粹的内部行为;一个例子是序列化)。为什么所有这些复杂性(使用 System.Collections.Emit 非常困难;它需要很好的CIL知识,生成的代码很难调试)?



所以,我们来到 主题One-B 。它带给我们不同的想法:你可以建模属性的行为。最简单的机制是:例如,您可以创建类型的字典 System.Collections.Generic.Dictionary< string,PropertyType> ,其中 PropertyType value表示属性的类型,string key表示属性名称。几种不同的房产类型?好吧,你可以(再次,解决方案选择拆分)使用几个字典,每个属性类型一个,或使用 polymorphic (按属性类型)字典表示混合类型的属性集;并且您仍然可以使用不同的基类型来使用不同字典类型的不同实例,而不是对每个字典实例使用OOP方法。



这就是第一个问题;现在让我们返回 PropertyGrid 。它可以通过以下方式进行自定义:而不是 PropertyGrid.SelectedObject ,您应该不使用对象本身,而是使用某些其他对象来表示要在某些自定义中显示的对象办法;让我们说,它将是包装您想要显示的对象的对象。这个想法是:这个包装器对象应该实现设计用于与 PropertyGrid 协作的接口: System.ComponentModel.ICustomTypeDescriptor 。我在过去的回答中描述了这种技术: 如何详细信息和一些示例代码获取响应时单击PropertyGrid



使用此技术,您可以表示您想要的任何内容(包括使用 System.Collections.Emit 生成的类型的对象,以及模拟具有属性的对象实际建模属性行为的行为的对象。使用任何语义方式显示任何结构,不是一般的方式。



正如你所看到的,你面临着许多选择,层次结构,以及大量不太简单的工作,这部分是建筑你要做出的决定将取决于你想要达到的目标的细节(我猜你还没有确定),这些决定的后果将是非常有影响力的。只要面对它:这是不是每个人都可以携带的负载。但我不想劝阻你:它可以是一个非常好的编程水平,结果丰硕,如果不是非常成功的结果,不会完全浪费时间,这样的经验可能非常有用。







这个答案是继续 这里



-SA


据我所知,这是一个枚举类型属性,与 Alignment <完全相同/ code>(其他任何事情都没有意义)。好吧,你不需要任何特别的东西,只需写一个公共类型的公共财产。只有当您需要自定义此属性的演示时,您才需要付出更多努力。对于这种情况,我有几乎全面的关于枚举的系列文章,但可能只需要两个:

枚举类型不枚举!解决.NET和语言限制

人类可读的枚举元数据,在某些情况下可能是
,也可能是这个:按位枚举编辑器



-SA

I am new to WPF C#, I am trying to create a User Control with custom properties. I have a property with the name Planet.

And this property can take values as Mercury, Venus, Earth and Mars.

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

Can anyone advice me on how to do this?

解决方案

Member 10794476 asked:


I am able to do it as enumeration and it is static. If I want to generate the property values programatically at runtime, what would be the best possible way to do it?

There are two different questions here, second one depends first, but first is unrelated to second.

First one is about "dynamic properties". Roughly, this topic could also be subdivided in two. One-A topic is: you can generated code dynamically using System.Collections.Emit. But it is possible only when you generated the whole assembly. The approach without generation of assembly also exists: DynamicMethod, which is largely irrelevant to "properties". So, you generate the whole assembly, will all classes, structures, and all properties you may imagine. But what's the use? How to use these properties if they are dynamic? You want have any predefined compile-time interface to work with the code generated on the fly. Just think about it. By property names, or some descriptors (name + property type)? Possible, especially if the set of property types is a compile-time entities. But would it worth the effort?

I would say not, because, if you create such a machinery for accessing properties, it would outweigh the use of generated assemblies (which are actively used, but for implementation of purely internal behavior; one example is serialization). Why all these complexity (using System.Collections.Emit is quite difficult; it requires good knowledge of CIL and the generated code is quite hard to debug)?

So, we are coming to the topic One-B. It brings us to the different idea: you can model the behavior of properties. The simplest mechanism is this: for example, you can create dictionaries of the types System.Collections.Generic.Dictionary<string, PropertyType>, where PropertyType value represents the type of property, and string key represents the property name. Several different property types? Well, you can either (again, solution choices split) use several dictionaries, one per property type, or use polymorphic (by property type) dictionary representing set of properties of mixed type; and you still can have different instances of different dictionary type with different base types and than use OOP approach for each of the dictionary instances.

That's all on the first question; now let's return to the PropertyGrid. It can be customized in the following way: instead of PropertyGrid.SelectedObject, you should use not the object itself, but some other object representing the object you want to show in some customized way; let's say, it will be the object wrapping the object you want to show. The idea is: this wrapper object should implement the interface designed to collaborate with PropertyGrid: System.ComponentModel.ICustomTypeDescriptor. I describe this technique in my past answer: How to get response when click PropertyGrid, in detail and some sample code.

Using this technique, you can represent whatever you want (including the objects of types generated with System.Collections.Emit and the object "mocking" the behavior of "object with properties" actually modeling property behavior. You show any structure using any semantic ways, not in general way.

As you can see, you are facing a number of choices, hierarchically, and good amount of not very simple work, which is partly architectural. The decisions you are going to make will depend on the detail of what you want to achieve (I guess, you have yet to determine that, too), and the consequences of these decision would be very influential. Just face it: this is the load not everyone can carry. But I don't want to discourage you: it can be a really good level of programming with fruitful result, and in case of not very successful result, would not be a complete waste of time, such experience can be very useful.

[EDIT]

This answer is continued here

—SA


As I understand, this is an enumeration-type property, exactly as Alignment (anything else would simply make no sense). Well, you don't need anything special, just write a public property with public type. Only if you need to customize presentation of this property, you have to put a lot more effort. For such cases, I have a nearly comprehensive series of articles on enumerations, but may only need two:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations
and Human-readable Enumeration Meta-data,
in some cases, maybe also this one: Bitwise Enumeration Editor for PropertyGrid and Visual Studio.

—SA


这篇关于如何在设计时C#WPF期间显示自定义属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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