c# 自定义propertygrid
时间: 2023-11-29 14:02:49 浏览: 52
对于"C"来说,它有几个常见的含义和用途。
首先,C可以代表英语字母表中的第三个字母。在字母表中,C的发音是/k/。它在很多单词中被用作首字母,比如"cat"(猫)和"car"(车),还有一些常见的词汇如"cup"(杯子)和"cake"(蛋糕)。
其次,C也是一种编程语言的名称。C语言是一种通用的、高级的计算机编程语言,由美国计算机科学家丹尼斯·里奇于20世纪70年代早期开发。C语言具有高效、强大和灵活的特性,被广泛应用于各种计算机软件和应用程序的开发。许多其他的编程语言,例如C++和Java,都是从C语言发展而来的。
另外,C也是指摄氏度的单位。摄氏度是一种温度度量方式,常用来测量物体的温度。在摄氏度下,水的冰点为0℃,沸点为100℃。相比于华氏度(Fahrenheit),摄氏度在全球范围更常用。
总的来说,无论是字母表中的字母C,编程语言C,还是摄氏度C,它们都在不同的领域中有着重要的意义和应用。无论是通过书写和阅读,还是通过编程和测量温度,我们都会与C这个字母和它所代表的含义有所接触。
相关问题
c# PropertyGrid 自定义多层显示 示例
首先,我们需要创建一个自定义类来作为 PropertyGrid 的对象。这个类可以包含多个属性以及子属性。例如:
```c#
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
}
```
接下来,我们需要创建一个自定义的 PropertyDescriptor 类,用于控制 PropertyGrid 的显示方式。这个类需要继承自 PropertyDescriptor 类,并重写几个方法,包括 GetValue()、SetValue()、CanResetValue()、ResetValue()、ShouldSerializeValue() 等方法。例如:
```c#
public class NestedPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor _parent;
private PropertyDescriptor _child;
public NestedPropertyDescriptor(PropertyDescriptor parent, PropertyDescriptor child)
: base(child.Name, null)
{
_parent = parent;
_child = child;
}
public override object GetValue(object component)
{
object parentValue = _parent.GetValue(component);
if (parentValue == null)
return null;
return _child.GetValue(parentValue);
}
public override void SetValue(object component, object value)
{
object parentValue = _parent.GetValue(component);
if (parentValue == null)
return;
_child.SetValue(parentValue, value);
OnValueChanged(component, EventArgs.Empty);
}
public override bool CanResetValue(object component)
{
object parentValue = _parent.GetValue(component);
if (parentValue == null)
return false;
return _child.CanResetValue(parentValue);
}
public override void ResetValue(object component)
{
object parentValue = _parent.GetValue(component);
if (parentValue == null)
return;
_child.ResetValue(parentValue);
OnValueChanged(component, EventArgs.Empty);
}
public override bool ShouldSerializeValue(object component)
{
object parentValue = _parent.GetValue(component);
if (parentValue == null)
return false;
return _child.ShouldSerializeValue(parentValue);
}
public override Type ComponentType
{
get { return _parent.ComponentType; }
}
public override bool IsReadOnly
{
get { return _child.IsReadOnly; }
}
public override Type PropertyType
{
get { return _child.PropertyType; }
}
public override string DisplayName
{
get { return _child.DisplayName; }
}
}
```
最后,我们需要创建一个自定义的 TypeConverter 类,用于控制 PropertyGrid 的显示方式。这个类需要继承自 ExpandableObjectConverter 类,并重写几个方法,包括 GetProperties()、GetPropertiesSupported() 等方法。例如:
```c#
public class NestedTypeConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection properties = base.GetProperties(context, value, attributes);
List<PropertyDescriptor> nestedProperties = new List<PropertyDescriptor>();
foreach (PropertyDescriptor property in properties)
{
if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
{
PropertyDescriptorCollection nestedProps = TypeDescriptor.GetProperties(property.PropertyType, new Attribute[] { new BrowsableAttribute(true) });
foreach (PropertyDescriptor nestedProp in nestedProps)
{
nestedProperties.Add(new NestedPropertyDescriptor(property, nestedProp));
}
}
else
{
nestedProperties.Add(property);
}
}
return new PropertyDescriptorCollection(nestedProperties.ToArray());
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
```
最后,我们可以在窗体中使用 PropertyGrid 控件来显示我们自定义的类。例如:
```c#
private void Form1_Load(object sender, EventArgs e)
{
PropertyGrid1.SelectedObject = new Person
{
Name = "John",
Age = 30,
Address = new Address
{
Street = "123 Main St",
City = "Anytown",
State = "CA"
}
};
PropertyGrid1.BrowsableAttributes = new Attribute[] { new BrowsableAttribute(true) };
PropertyGrid1.ExpandAllGridItems();
}
```
以上就是 C# PropertyGrid 自定义多层显示的示例。希望能对你有所帮助。
c# PropertyGrid 多层折叠 示例
在 C# 中,PropertyGrid 控件默认只支持一级折叠,也就是说,只能将对象的属性分组显示,但不能将分组再次折叠。如果你需要实现多层折叠,可以使用以下方法:
首先,我们需要创建一个自定义类型,用于表示一个折叠组:
```csharp
public class PropertyGridGroup
{
public string Name { get; set; }
public object[] Properties { get; set; }
}
```
该类型包含一个 Name 属性和一个 Properties 属性,其中 Properties 属性是一个对象数组,用于存储该折叠组包含的属性。
接下来,我们可以为每个折叠组创建一个自定义属性描述符,并将其添加到 PropertyGrid 的 PropertyDescriptors 集合中。每个属性描述符都应该返回一个 PropertyGridGroup 对象的 Properties 属性作为其子属性。这样,我们就可以实现多层折叠了。
以下是一个示例,包含两个折叠组,每个折叠组包含两个属性:
```csharp
public class MyPropertyGrid : PropertyGrid
{
protected override PropertyDescriptorCollection GetProperties(object component, Attribute[] attributes)
{
PropertyDescriptorCollection properties = base.GetProperties(component, attributes);
// Create groups
PropertyGridGroup group1 = new PropertyGridGroup
{
Name = "Group 1",
Properties = new object[]
{
properties["Property1"],
properties["Property2"]
}
};
PropertyGridGroup group2 = new PropertyGridGroup
{
Name = "Group 2",
Properties = new object[]
{
properties["Property3"],
properties["Property4"]
}
};
// Create custom property descriptors for the groups
PropertyDescriptor[] descriptors = new PropertyDescriptor[]
{
new CustomPropertyDescriptor(group1, "Group 1"),
new CustomPropertyDescriptor(group2, "Group 2")
};
// Combine the custom descriptors with the original ones
PropertyDescriptorCollection result = new PropertyDescriptorCollection(descriptors);
foreach (PropertyDescriptor prop in properties)
{
if (prop.Name != "Property1" && prop.Name != "Property2" && prop.Name != "Property3" && prop.Name != "Property4")
{
result.Add(prop);
}
}
return result;
}
}
public class CustomPropertyDescriptor : PropertyDescriptor
{
private object _component;
private object[] _properties;
public CustomPropertyDescriptor(object component, string name) : base(name, null)
{
_component = component;
_properties = new object[1] { component };
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
return _component;
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return typeof(object); }
}
public override bool IsReadOnly
{
get { return true; }
}
public override Type PropertyType
{
get { return typeof(object[]); }
}
public override AttributeCollection Attributes
{
get { return new AttributeCollection(null); }
}
public override string DisplayName
{
get { return base.Name; }
}
public override string Description
{
get { return base.Name; }
}
public override string Category
{
get { return ""; }
}
public override object[] GetChildProperties(object instance, Attribute[] filter)
{
return _properties;
}
public override bool IsBrowsable
{
get { return true; }
}
}
```
使用上面的代码,我们可以创建一个名为 MyPropertyGrid 的自定义 PropertyGrid 控件,该控件支持多层折叠。下面是一个示例:
```csharp
MyPropertyGrid propertyGrid = new MyPropertyGrid();
propertyGrid.Dock = DockStyle.Fill;
propertyGrid.SelectedObject = new
{
Property1 = "Value1",
Property2 = "Value2",
Property3 = "Value3",
Property4 = "Value4"
};
this.Controls.Add(propertyGrid);
```
在上面的示例中,我们创建了一个 MyPropertyGrid 控件,并将其添加到窗体中。然后,我们将一个匿名对象分配给 SelectedObject 属性,以便在 PropertyGrid 中显示四个属性。这四个属性将根据其所属的折叠组层次结构进行分组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)