百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

Avalonia.PropertyGrid

> 编程语言
开源

Avalonia 中的属性编辑控件,类似于 DevExpress 的 PropertyGridControl。

336 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Avalonia 中的属性编辑控件,类似于 DevExpress 的 PropertyGridControl。

Avalonia.PropertyGrid

查看简体中文文档

This is a PropertyGrid implementation for Avalonia, you can use it in Avalonia Applications.
Its main features are:

  • Support automatic analysis of class's Properties like DevExpress's PropertyGridControl and display and edit
  • Support simultaneous editing of multiple objects in one PropertyGrid
  • Support custom types that implement the ICustomTypeDescriptor interface or TypeDescriptionProvider
  • Support custom property label controls
  • Support custom property cell edit
  • Support for additional property operation areas and their custom operations
  • Support collections editing, support for adding, inserting, deleting and clearing in the control
  • Support data verification
  • Support Built-in undo and redo framework
  • Support global ReadOnly setting
  • Support for automatically adjusting the visibility of properties based on conditions
  • Support path picking
  • Support three display modes: category-based, alphabetical sorting and builtin sorting
  • Support text filtering, regular expression filtering, and supports ignoring case settings
  • Support fast filtering by Category
  • Support data automatic reloading
  • Support automatic expansion of sub-objects
  • Support adjust the width of the property name and property value by drag the title
  • Support localization and realtime change language without restart
  • Support web

→ View Online Demo

How To Use

Use the source code of this project directly or use NUGET Packages:
https://www.nuget.org/packages/bodong.Avalonia.PropertyGrid
Then add PropertyGrid to your project, and bind the object to be displayed and edited to the DataContext property. If you want to bind multiple objects, just bind IEnumerable directly.
An example of a minimal template project based on Avalonia:

…
xml

  
    
    
  

    
    

How to Debug

Open Avalonia.PropertyGrid.sln, change config to Development, set Avalonia.PropertyGrid.Samples.Desktop as startup project, build and run it.

How to build Online Demo

run dotnet publish under Samples/Avalonia.PropertyGrid.Samples.Browser. This may end up taking several minutes.
Because of the bug in .net 8.0, you must use .net 9.0, otherwise you will encounter errors when running

Detail Description

Data Modeling

If you want to edit an object in PropertyGrid, you only need to directly set this object to the DataContext property of PropertyGrid, PropertyGrid will automatically analyze the properties that can support editing, and edit it with the corresponding CellEdit. At the same time, you can also use Attributes in System.ComponentModel, System.ComponentModel.DataAnnotations and PropertyModels.ComponentModel to mark these properties, so that these properties have some special characteristics.
Support but not limited to these:

…

In addition, there are other classes that can be supported in PropertyModels.ComponentModel and PropertyModels.ComponentModel.DataAnnotations, which can assist in describing class properties.
If you want to have some associations between your class properties, for example, some properties depend on other properties in implementation, then you can try to mark this dependency with PropertyModels.ComponentModel.DataAnnotations.DependsOnPropertyAttribute
but you need to inherit your class from PropertyModels.ComponentModel.ReactiveObject, otherwise you need to maintain this relationship by yourself, just trigger the PropertyChanged event of the target property when the dependent property changes.

…

Supported Builtin Types

…

By default, structure properties are not supported. All structure properties need to be customized before they can be displayed.

Extra Data Structure

  • PropertyModels.Collections.SelectableList
    You can initialize this list with some objects, and you can only select one object in this list. ProeprtyGrid uses ComboBox by default to edit the properties of this data structure
  • PropertyModels.Collections.CheckedList like SelectableList, you can initialize it with some objects, but you can select multiple objects in it. ProeprtyGrid uses a set of CheckBoxes by default to edit the properties of this data structure.

Data Reloading

Implement from System.ComponentModel.INotifyPropertyChanged and trigger the PropertyChanged event when the property changes. PropertyGrid will listen to these events and automatically refresh the view data.
if you implementing from PropertyModels.ComponentModel.INotifyPropertyChanged instead of System.ComponentModel.INotifyPropertyChanged will gain the additional ability to automatically fire the PropertyChanged event when an edit occurs in the PropertyGrid without having to handle each property itself.
You can also directly inherit PropertyModel.ComponentModel.MiniReactiveObject, PropertyModel.ComponentModel.ReactiveObject. The former only has data change notification capabilities, while the latter also has data dynamic visibility refresh support. If you use ReactiveUI.ReactiveObject directly, then you will not have dynamic visibility support. At this time, you need to monitor the relevant properties yourself, rather than using the RaisePropertyChanged method to throw the corresponding property change event.

Custom Property Filter

xml

set CustomPropertyDescriptorFilter, and add your custom process.

csharp
private void OnCustomPropertyDescriptorFilter(object? sender, CustomPropertyDescriptorFilterEventArgs e)
{
	if(e.TargetObject is SimpleObject simpleObject&& e.PropertyDescriptor.Name == "ThreeStates2")
    {
        e.IsVisible = true;
        e.Handled = true;
    }
}

check MainView.axaml.cs for more information.

Change Size

You can change the width of namelabel and cell edit by drag here:

Or set the NameWidth property of PropertyGrid directly.

Multiple Objects Edit

If you want to edit multiple objects at the same time, you only need to set the object to DataContext as IEnumerable, for example:

csharp
public IEnumerable multiObjects => new SimpleObject[] { multiObject0, multiObject1 };
xml

Due to complexity considerations, there are many complex types of multi-object editing that are not supported!!!

Custom Object & Virtual Object

You can implement your own custom objects or virtual objects based on TypeDescriptionProvider and ICustomTypeDescriptor, and PropertyGrid will automatically recognize and use them.

Collection Support

PropertyGrid supports collection editing.
All containers implemented from IList will be automatically recognized and displayed, but only containers or types that implement IBindingList or INotifyCollectionChanged interface will support data synchronization, because data synchronization requires the property type to throw messages itself, and there is no way to help it from the outside.
Setting [Editable(false)] can disable the creation and deletion functions, making the original addable and deleteable container become the same as a normal array, only allowing display and modification, not allowing the addition and deletion of child elements.

**In addition, in order to support the creation function, the template parameter of the container can only be a non-pure virtual class. ** **Structural properties are not supported and need to be customized. **

Expandable Class Properties

When PropertyGrid does not provide a built-in CellEdit to edit the target property, there are several possibilities:

  1. If the property or the PropertyType of property is marked with TypeConverter, then the PropertyGrid will try to use the TextBox to edit the object. When the text is changed, it will actively try to use TypeConverter to convert the string into the target object.
  2. If the property uses ExpandableObjectConverter, then PropertyGrid will try to expand the object in place.
  3. If neither is satisfied, then PropertyGrid will try to use a read-only TextBox to display the ToString() value of the target property.
csharp
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class EncryptData : MiniReactiveObject
    {
        public EncryptionPolicy Policy { get; set; } = EncryptionPolicy.RequireEncryption;

        public RSAEncryptionPaddingMode PaddingMode { get; set; } = RSAEncryptionPaddingMode.Pkcs1;
    }

    #region Expandable
    [DisplayName("Expand Object")]
    [Category("Expandable")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public LoginInfo loginInfo { get; set; } = new LoginInfo();
    #endregion

Data Validation

There are two ways to provide data validation capabilities:

  1. Throw an exception directly in the setter of the property. But I personally don't recommend this method very much, because if you set this property in the code, it may cause errors by accident. like:
…
  1. The second method is to use System.ComponentModel.DataAnnotations.ValidationAttribute to mark the target property, both system-provided and user-defined. for example:
…

Dynamic Visibilty

By setting Attribute, you can make certain Properties only displayed when conditions are met. for example:

…

In this example, you can check IsShowPath first, then set the Platform to Unix, and then enter anything in UnixVersion, and you will see the unixLoginInfo field. To do this, you only need to mark the property with a custom Attribute. If you need to implement your own rules, just implement your own rules from.

The implementation behind this depends on IReactiveObject in PropertyModels, you can implement it yourself, or directly derive your Model from ReactiveObject.

AbstractVisiblityConditionAttribute.
One thing to pay special attention to is that any property that needs to be used as a visibility condition for other properties needs to be marked with [ConditionTarget].
The purpose is to let PropertyGrid know that when this property changes, it needs to notify the upper layer to refresh the visibility information.

User Localization

Implement your PropertyModels.Services.ILocalizationService class, and register its instance by :

csharp
    LocalizationService.Default.AddExtraService(new YourLocalizationService());

If you want to provide the corresponding language pack for the built-in text, please add the corresponding file to /Sources/Avalonia.PropertyGrid/Assets/Localizations, and name it with the CultureInfo.Name of the language. for example:

    en-US.json
    ru-RU.json
    zh-CN.json

Custom Property Label Controls

To custom label control, use CustomNameBlock event on PropertyGrid, and assign your custom control to e.CustomNameBlock, for example:

…

Custom Cell Edit

To customize a CellEdit, you need to implement a new factory class from AbstractCellEditFactory or other existing factory classes, and then register an instance of this factory class with the PropertyGrid, for example:

csharp
public class TestExtendPropertyGrid : Controls.PropertyGrid
{
    public TestExtendPropertyGrid()
    {
        Factories.AddFactory(new Vector3CellEditFactory());
        Factories.AddFactory(new CountryInfoCellEditFactory());
        Factories.AddFactory(new ToggleSwitchCellEditFactory());

        // ...
    }
}

To implement a custom factory class, you may need to focus on the following methods that can be overridden. The first two are mandatory to override, while the others are optional:

  1. HandleNewProperty is used to create the control you want to use to edit the property. You need to pass the value through the framework's interface after editing the data in the UI to ensure that other related objects receive the notification and save the undo/redo command.
…
  1. HandlePropertyChanged m

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言