Table of Contents

Property Grid

There is a style including some extensional functionality for the third party PropertyGrid from PropertyTools. For usage set the style key LoehnertPropertyGrid on a PropertyTools.PropertyGrid.

Features

Booleans

Properties of type bool are shown as check box or as Indicator if the property is read-only. The color of the indicator depends on the property name.

Keys in property name Indicator Color
Warning Yellow
Fault, Error Red

Commands

Properties of type ICommand are shown as BusyButton.

Attributes

This list shows some attributes to customize the view of a property. You can find more attributes here.

Attribute What it does Example
ReadOnlyAttribute Makes the property read-only. [ReadOnly(isReadOnly: true)]
BrowsableAttribute Hides the property. [Browsable(browsable:false)]
DataTypeAttribute Sets the type of the value. [DataType(DataType.Url)] or [DataType(DataType.MultilineText)]
DisplayAttribute Translates the property. [Display(Name = nameof(MyApplication.Resources.NameOfAResource), ResourceType = typeof(MyApplication.Resources))]
DisplayFormatAttribute Defines a format string for the value. [DisplayFormat(DataFormatString = "{0} mm")]
RangeAttribute Defines a value range for validation. [Range(minimum: 0, maximum: 5)]
AutoUpdateTextAttribute Sets the UpdateSourceTrigger to PropertyChanged, only relevant for TextBoxes. [PropertyTools.DataAnnotations.AutoUpdateText]
CommentAttribute Specifies that the value is a comment. [PropertyTools.DataAnnotations.Comment]

Validation

Properties can be validated by the UI by using the IDataErrorInfo interface. Use the PropertyGridViewModelBase for a fast implementation.

This example shows how to validate with the PropertyGridViewModelBase class.

internal static class MyPropertiesViewModel : PropertyGridViewModelBase
{
    public string Text { get; set; }

    public override string this[string columnName]
    {
        get
        {
            // Call the base to keep the support for validation attributes e. g. the RangeAttribute.
            var errorTextBuilder = new StringBuilder(base[columnName]);

            // Any custom validation.
            if (Text.Contains(" "))
                errorTextBuilder.AppendLine("Text can not have white space."); // ToDo: Use a translated text.

            return errorTextBuilder.ToString();
        }
    }
}

Example

// For PropertyChanged and IDataErrorInfo validation functionality,
// use the Loehnert.Lisrt.WpfHelpers.PropertyGridViewModelBase.
internal class SomePropertiesViewModel : PropertyGridViewModelBase
{
    public SomePropertiesViewModel()
    {
        ClearTextCommand = new RelayCommand(_ => Text = string.Empty);
    }
    
    public bool BooleanValue { get; set; } = true;

    [ReadOnly(true)]
    public bool ReadonlyBooleanValue { get; } = true;

    [Display(Name = nameof(Resources.AnyValueName), ResourceType = typeof(Resources))]
    public double AnyValue { get; set; }
    
    [DisplayFormat(DataFormatString = "{0}\u2009mm")]
    [Range(0, 5)]
    public double LimitedValueWithUnit { get; set; }

    [ReadOnly(true)]
    public double ReadonlyValue {get; set;}

    public Fruits TranslatedEnumValue { get; set; }

    public string Text { get; set; }

    public ICommand ClearTextCommand { get; }
}

[TypeConverter(typeof(Loehnert.Utility.Converters.EnumTranslationConverter))]
internal enum Fruits
{
    [Display(Name = nameof(Resources.Banana), ResourceType = typeof(Resources))]
    Banana,

    [Display(Name = nameof(Resources.Apple), ResourceType = typeof(Resources))]
    Apple,

    [Display(Name = nameof(Resources.Cherry), ResourceType = typeof(Resources))]
    Cherry,
}