using Avalonia.Controls; using Avalonia.LogicalTree; using AdvancedExample.Models; namespace AdvancedExample; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var config = new AdvancedConfiguration(); DataContext = config; // Find the PropertyGrid and set its SelectedObject directly this.Opened += (s, e) => { var propertyGrid = this.FindDescendantOfType(); if (propertyGrid != null) { propertyGrid.SelectedObject = config; System.Diagnostics.Debug.WriteLine("AdvancedExample: PropertyGrid found and SelectedObject set directly"); } else { System.Diagnostics.Debug.WriteLine("AdvancedExample: PropertyGrid NOT found!"); } }; } } public static class ControlExtensions { public static T? FindDescendantOfType(this Control control) where T : class { if (control is T result) return result; foreach (var child in control.GetLogicalChildren()) { if (child is Control childControl) { var found = FindDescendantOfType(childControl); if (found != null) return found; } } return null; } }