53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using Avalonia.Controls;
|
|||
|
|
using Avalonia.Controls.Primitives;
|
||
|
|
using Avalonia.LogicalTree;
|
||
|
|
using QuickStartExample.Models;
|
||
|
|
|
||
|
|
namespace QuickStartExample;
|
||
|
|
|
||
|
|
public partial class MainWindow : Window
|
||
|
|
{
|
||
|
|
public MainWindow()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
var vehicle = new Vehicle();
|
||
|
|
DataContext = vehicle;
|
||
|
|
|
||
|
|
// Find the PropertyGrid and set its SelectedObject directly
|
||
|
|
this.Opened += (s, e) =>
|
||
|
|
{
|
||
|
|
var propertyGrid = this.FindDescendantOfType<Avalonia.PropertyGrid.Controls.PropertyGrid>();
|
||
|
|
if (propertyGrid != null)
|
||
|
|
{
|
||
|
|
propertyGrid.SelectedObject = vehicle;
|
||
|
|
System.Diagnostics.Debug.WriteLine("PropertyGrid found and SelectedObject set directly");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine("PropertyGrid NOT found!");
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static class ControlExtensions
|
||
|
|
{
|
||
|
|
public static T? FindDescendantOfType<T>(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<T>(childControl);
|
||
|
|
if (found != null)
|
||
|
|
return found;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|