Files
PropertyGrid/COLOR_PROPERTY_ADDED.md
T
2026-08-10 14:46:18 +02:00

6.5 KiB

Color Property Added to AdvancedExample

Summary

Added a color property to the AdvancedExample to demonstrate how the PropertyGrid handles Avalonia Color types.

Changes Made

1. Added Color Property to AdvancedConfiguration

File: Examples/AdvancedExample/Models/AdvancedConfiguration.cs

New Using Directive

using Avalonia.Media;

New Private Field

private Color _themeColor = Colors.DodgerBlue;

New Properties

[Display(Name = "Theme Color", Description = "Application theme accent color", Order = 44)]
[Category("Appearance")]
public Color ThemeColor
{
	get => _themeColor;
	set
	{
		if (SetProperty(ref _themeColor, value))
		{
			OnPropertyChanged(nameof(ThemeColorName));
		}
	}
}

[Display(Name = "Theme Color Name", Description = "Name of the current theme color", Order = 45)]
[Category("Appearance")]
[ReadOnly(true)]
public string ThemeColorName => _themeColor.ToString();

2. Updated Documentation

README.md Updates

  • Added "Appearance" to categories list (now 8 categories)
  • Added Color to data types list
  • Added ThemeColorName to read-only properties list

MainWindow.axaml Header Updates

  • Updated category count: "Categories: 7" → "Categories: 8"
  • Updated property count: "Properties: 25+" → "Properties: 27+"

New Features Demonstrated

Theme Color Property

  • Type: Avalonia.Media.Color
  • Default Value: Colors.DodgerBlue (blue accent color)
  • Category: Appearance
  • Editable: Yes
  • Display Order: 44

Theme Color Name Property

  • Type: string
  • Value: Computed from ThemeColor.ToString()
  • Category: Appearance
  • Editable: No (Read-only)
  • Display Order: 45
  • Auto-updates: When ThemeColor changes

Property Interactions

When the user changes ThemeColor:

  1. The ThemeColor setter calls SetProperty()
  2. If value changed, OnPropertyChanged(nameof(ThemeColorName)) is called
  3. ThemeColorName computed property refreshes automatically
  4. UI updates both properties via INotifyPropertyChanged

Current Display in PropertyGrid

Since the PropertyGrid currently uses a simple TextBox editor for all properties:

ThemeColor will display as:

Theme Color: [#FF1E90FF]   (example ARGB hex value)

ThemeColorName will display as:

Theme Color Name: #FF1E90FF   (read-only, auto-updates)

Future Enhancements

When specialized editors are added, the color property could use:

Option 1: ColorPicker Control

<ColorPicker Color="{Binding Value, Mode=TwoWay}" />

Option 2: Color Preview with TextBox

<StackPanel Orientation="Horizontal">
	<Border Width="24" Height="24" 
			Background="{Binding Value, Converter={StaticResource ColorToBrushConverter}}"
			BorderBrush="Gray" BorderThickness="1" 
			CornerRadius="3" Margin="0,0,8,0"/>
	<TextBox Text="{Binding Value, Mode=TwoWay}" />
</StackPanel>

Option 3: Predefined Color Palette

<ComboBox ItemsSource="{x:Static Colors.All}"
		  SelectedItem="{Binding Value, Mode=TwoWay}">
	<ComboBox.ItemTemplate>
		<DataTemplate>
			<StackPanel Orientation="Horizontal">
				<Border Width="16" Height="16" Background="{Binding .}" />
				<TextBlock Text="{Binding .}" Margin="8,0,0,0"/>
			</StackPanel>
		</DataTemplate>
	</ComboBox.ItemTemplate>
</ComboBox>

Testing

Build Status

✅ Builds successfully - No compilation errors

To Test

dotnet run --project Examples/AdvancedExample/AdvancedExample.csproj

What to Look For

  1. Scroll to the "Appearance" category (new category)
  2. Find "Theme Color" property
  3. See current value (looks like #FF1E90FF for DodgerBlue)
  4. Try editing the hex value (e.g., change to #FFFF0000 for red)
  5. Notice "Theme Color Name" updates automatically below it (read-only)

Valid Color Format Examples

The TextBox accepts various formats:

  • Hex ARGB: #FFFF0000 (red with full opacity)
  • Hex RGB: #FF0000 (red, opacity defaults to FF)
  • Named Colors: Try typing color names (if supported by ToString/Parse)

Statistics

Updated Counts

Metric Before After
Categories 7 8 (added Appearance)
Properties ~25 ~27 (added ThemeColor + ThemeColorName)
Property Types 9 10 (added Color)
Read-Only Properties 4 5 (added ThemeColorName)
Computed Properties 1 2 (added ThemeColorName)

Categories

  1. Server Configuration
  2. Logging
  3. Network
  4. Database
  5. Security
  6. Performance
  7. Appearance ⭐ NEW
  8. Metadata

Code Quality

Best Practices Followed

✅ Added using directive for Avalonia.Media
✅ Used backing field with proper naming (_themeColor)
✅ Implemented INotifyPropertyChanged correctly
✅ Added Display attribute with name, description, and order
✅ Added Category attribute for logical grouping
✅ Made computed property read-only
✅ Used cascading property notifications (ThemeColor→ThemeColorName)
✅ Set reasonable default value (Colors.DodgerBlue)
✅ Updated documentation to reflect changes

Pattern Consistency

The ThemeColor/ThemeColorName pair follows the same pattern as:

  • ConnectionType/RequiresCertificate
  • Both demonstrate computed read-only properties

Files Modified

File Changes
Examples/AdvancedExample/Models/AdvancedConfiguration.cs Added Color property and ThemeColorName
Examples/AdvancedExample/README.md Updated categories and data types
Examples/AdvancedExample/MainWindow.axaml Updated header statistics

Color Property Benefits

Demonstrates

  1. Complex Types: Color is a struct, showing how PropertyGrid handles structs
  2. Computed Properties: ThemeColorName auto-updates from ThemeColor
  3. Property Relationships: Changing one property affects another
  4. Category Organization: New "Appearance" category for UI-related settings
  5. Read-Only Derived Values: ThemeColorName can't be edited directly

Use Cases

  • Application theme customization
  • Branding color configuration
  • Visual settings management
  • UI customization options

Next Steps

Immediate

The color property works with the current TextBox editor (hex string input).

Future (When Specialized Editors Added)

  1. Add ColorPicker editor for Color type
  2. Show color preview swatch
  3. Add predefined color palette
  4. Support color name lookup
  5. Add opacity/alpha slider

Status: ✅ Color property successfully added and functional!
Demo: Run AdvancedExample and find "Appearance" category with Theme Color properties