📄
OutGridTree.Window/MainWindow.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System; using System.Collections.ObjectModel; using System.Management.Automation; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Data.Converters; namespace OutGridTree.Window; internal sealed partial class MainWindow : Avalonia.Controls.Window { private readonly RpcService rpcService; private readonly ObservableCollection<object?> records = []; public MainWindow(RpcService rpcService) { this.rpcService = rpcService; rpcService.RecordReceived += OnRecordReceived; InitializeComponent(); Table.ItemsSource = records; } private void OnRecordReceived(object? record) { Dispatcher.Post(() => AddRecord(record)); } private void AddRecord(object? record) { if (record is not null && records.Count is 0) { if (record is PSObject psObject) { foreach (var property in psObject.Properties) { var name = property.Name; Table.Columns.Add( new DataGridTextColumn() { Header = name, Binding = CompiledBinding.Create<object, object>( o => o, converter: new FuncValueConverter<object, string?>(o => (o as PSObject)?.Properties[name].Value?.ToString() ) ), } ); } } else { Table.Columns.Add( new DataGridTextColumn() { Header = record switch { bool => "Boolean", byte or sbyte or short or ushort or int or uint or long or ulong => "Integer", float or double or decimal => "Number", char => "Character", string => "String", _ => "Primitive", }, Binding = CompiledBinding.Create<object, object>( o => o, converter: new FuncValueConverter<object, string?>(o => o?.ToString()) ), } ); } } records.Add(record); } protected override void OnClosed(EventArgs e) { rpcService.RecordReceived -= OnRecordReceived; base.OnClosed(e); } }