净属性网格。有没有办法让网格操作对象不同的方式 [英] .Net Property Grid. Is there a way to let the Grid manipulate object in different way
问题描述
据我了解,在属性网格被赋予一个对象,它可以通过提取利用反射的特性操纵。
As I understood , The property grid is given an object which it can manipulate by extracting its Properties using reflections.
我的问题是,我有一个在运行时确定的一组参数,因此我不能staticly组成一个类的属性重新present这一套。
My problem is that I have a set of Parameters that is determined during run-time , thus I can't staticly compose a class with properties to represent this set.
我有两个想法在头脑来解决这个问题,但无论是复杂的,可能会耗费大量的时间,事实上我会说他们不是在我的时间限制实用。一种是使用反射发出,以动态地定义一个类,另一个是dynamiclly建立一个C#源文件,然后使用codeDOM编译。
I have two idea in mind to solve this problem but both are complex and will probably consume lot of time , infact i will say they are not practical under my time constraints. One is to use Reflection Emit in order to define a class dynamically and the other is to dynamiclly build a C# source file and then compile it using CodeDom.
能否属性网格的行为以不同的方式(其他然后提取使用反射物体的性质),可以适合我的问题?
Can Property grid behave in a different manner( other then extracting the Properties of an object using reflections ) that can suite my problem?
如果没有你知不知道任何其他控件,可以做的工作给我吗?
If no do you know any other control that can do the job for me?
我想说的是,我去了属性网格从一开始的原因是它能够提供真的很好的数据检索界面,共同types.For颜色,你autometically得到一个调色板,对于DATATIME你自动拥有一个漂亮的日历功能。我想自动把这些东西,如果可能的话。
I want to say that the reason I went to the property grid from the begining was its ability to provide realy nice Data Retrieval UI for common types.For color you autometically get a palette , For dataTime you automatically have a nice calender. I would like to get those things automatically , If possible.
推荐答案
是的PropertyGrid
能显示东西的等的不仅仅是编译时性能,使用任何的类型转换器
, ICustomTypeDescriptor
或 TypeDescriptionProvider
提供运行时的伪属性。你能不能给一个什么样的参数看起来像一个例子吗?我应该能够提供一个例子...
Yes, PropertyGrid
can display things other than just the compile-time properties, by using any of TypeConverter
, ICustomTypeDescriptor
or TypeDescriptionProvider
to provide runtime pseudo-properties. Can you give an example of what your parameters look like? I should be able to provide an example...
下面是一个基本的例子(一切都是字符串
等)的基础上的刚才的答复(相关但不同的):
here's a basic example (everything is string
, etc) based on an earlier reply (related but different):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
class PropertyBagPropertyDescriptor : PropertyDescriptor {
public PropertyBagPropertyDescriptor(string name) : base(name, null) { }
public override object GetValue(object component) {
return ((PropertyBag)component)[Name];
}
public override void SetValue(object component, object value) {
((PropertyBag)component)[Name] = (string)value;
}
public override void ResetValue(object component) {
((PropertyBag)component)[Name] = null;
}
public override bool CanResetValue(object component) {
return true;
}
public override bool ShouldSerializeValue(object component)
{ // *** this controls whether it appears bold or not; you could compare
// *** to a default value, or the last saved value...
return ((PropertyBag)component)[Name] != null;
}
public override Type PropertyType {
get { return typeof(string); }
}
public override bool IsReadOnly {
get { return false; }
}
public override Type ComponentType {
get { return typeof(PropertyBag); }
}
}
[TypeConverter(typeof(PropertyBagConverter))]
class PropertyBag {
public string[] GetKeys() {
string[] keys = new string[values.Keys.Count];
values.Keys.CopyTo(keys, 0);
Array.Sort(keys);
return keys;
}
private readonly Dictionary<string, string> values
= new Dictionary<string, string>();
public string this[string key] {
get {
string value;
values.TryGetValue(key, out value);
return value;
}
set {
if (value == null) values.Remove(key);
else values[key] = value;
}
}
}
// has the job of (among other things) providing properties to the PropertyGrid
class PropertyBagConverter : TypeConverter {
public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
return true; // are we providing custom properties from here?
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes) {
// get the pseudo-properties
PropertyBag bag = (PropertyBag)value;
string[] keys = bag.GetKeys();
PropertyDescriptor[] props = Array.ConvertAll(
keys, key => new PropertyBagPropertyDescriptor(key));
return new PropertyDescriptorCollection(props, true);
}
}
static class Program {
[STAThread]
static void Main() { // demo form app
PropertyBag bag = new PropertyBag();
bag["abc"] = "def";
bag["ghi"] = "jkl";
bag["mno"] = "pqr";
Application.EnableVisualStyles();
Application.Run(
new Form {
Controls = { new PropertyGrid {
Dock = DockStyle.Fill,
SelectedObject = bag
}}
});
}
}
这篇关于净属性网格。有没有办法让网格操作对象不同的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!