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
Colorto data types list - Added
ThemeColorNameto 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:
- The
ThemeColorsetter callsSetProperty() - If value changed,
OnPropertyChanged(nameof(ThemeColorName))is called ThemeColorNamecomputed property refreshes automatically- 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
- Scroll to the "Appearance" category (new category)
- Find "Theme Color" property
- See current value (looks like
#FF1E90FFfor DodgerBlue) - Try editing the hex value (e.g., change to
#FFFF0000for red) - 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
- Server Configuration
- Logging
- Network
- Database
- Security
- Performance
- Appearance ⭐ NEW
- 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
- Complex Types: Color is a struct, showing how PropertyGrid handles structs
- Computed Properties: ThemeColorName auto-updates from ThemeColor
- Property Relationships: Changing one property affects another
- Category Organization: New "Appearance" category for UI-related settings
- 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)
- Add ColorPicker editor for Color type
- Show color preview swatch
- Add predefined color palette
- Support color name lookup
- Add opacity/alpha slider
Status: ✅ Color property successfully added and functional!
Demo: Run AdvancedExample and find "Appearance" category with Theme Color properties