Avalonia 中的属性编辑控件,类似于 DevExpress 的 PropertyGridControl。
Avalonia 中的属性编辑控件,类似于 DevExpress 的 PropertyGridControl。
This is a PropertyGrid implementation for Avalonia, you can use it in Avalonia Applications.
Its main features are:
ReadOnly setting→ View Online Demo
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:
…
Open Avalonia.PropertyGrid.sln, change config to Development, set Avalonia.PropertyGrid.Samples.Desktop as startup project, build and run it.
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
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.
……By default, structure properties are not supported. All structure properties need to be customized before they can be displayed.
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.
set CustomPropertyDescriptorFilter, and add your custom process.
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.
You can change the width of namelabel and cell edit by drag here:
Or set the NameWidth property of PropertyGrid directly.
If you want to edit multiple objects at the same time, you only need to set the object to DataContext as IEnumerable, for example:
public IEnumerable multiObjects => new SimpleObject[] { multiObject0, multiObject1 };Due to complexity considerations, there are many complex types of multi-object editing that are not supported!!!
You can implement your own custom objects or virtual objects based on TypeDescriptionProvider and ICustomTypeDescriptor, and PropertyGrid will automatically recognize and use them.
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. **
When PropertyGrid does not provide a built-in CellEdit to edit the target property, there are several possibilities:
[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();
#endregionThere are two ways to provide data validation capabilities:
…System.ComponentModel.DataAnnotations.ValidationAttribute to mark the target property, both system-provided and user-defined. for example:…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.
Implement your PropertyModels.Services.ILocalizationService class, and register its instance by :
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.jsonTo custom label control, use CustomNameBlock event on PropertyGrid, and assign your custom control to e.CustomNameBlock, for example:
…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:
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:
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.…HandlePropertyChanged m暂无开放 Issues,或尚未同步最近议题。