-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
75 lines (65 loc) · 3.05 KB
/
MainWindowViewModel.cs
File metadata and controls
75 lines (65 loc) · 3.05 KB
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
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using FlatTreeDataGridSample.Models;
namespace FlatTreeDataGridSample.ViewModels;
/// <summary>
/// Represents the view model for the main window, providing a data source for a flat TreeDataGrid
/// that displays a collection of <see cref="Country"/> objects.
/// </summary>
/// <remarks>
/// The data grid source includes predefined columns for displaying country information such as
/// name, region, population, area, and GDP. The columns support features like text search and
/// custom templates for specific fields.
/// </remarks>
internal partial class MainWindowViewModel : ViewModelBase
{
private readonly ObservableCollection<Country> _data = new(Countries.All);
public MainWindowViewModel()
{
// Create a flat TreeDataGrid source and define the columns to show.
Source = new FlatTreeDataGridSource<Country>(_data)
{
Columns =
{
// Define a row header column for row numbering. This can be made to be always
// visible when scrolling horizontally by setting FrozenColumnCount = 1 on the
// TreeDataGrid.
new RowHeaderColumn<Country>(),
// Define a read/write text column for the country name with text search enabled.
new TextColumn<Country, string>(
"Country",
x => x.Name,
(o, v) => o.Name = v,
new GridLength(6, GridUnitType.Star), new()
{
IsTextSearchEnabled = true,
}),
// Define a template column for the region with custom cell and edit templates.
new TemplateColumn<Country>("Region", "RegionCell", "RegionEditCell"),
// Define read-only text columns for population and area.
new TextColumn<Country, int>("Population", x => x.Population, new GridLength(3, GridUnitType.Star)),
new TextColumn<Country, int>("Area", x => x.Area, new GridLength(3, GridUnitType.Star)),
// Define a read-only text column for GDP with right-aligned text and a maximum width.
new TextColumn<Country, int>("GDP", x => x.Gdp, new GridLength(3, GridUnitType.Star), new()
{
TextAlignment = Avalonia.Media.TextAlignment.Right,
MaxWidth = new GridLength(150)
}),
}
};
Source.Selection = new TreeDataGridCellSelectionModel<Country>(Source);
MaxPopulation = _data.Max(x => x.Population);
}
/// <summary>
/// Gets the data source for the flat tree data grid, containing a collection of
/// <see cref="Country"/> objects.
/// </summary>
public FlatTreeDataGridSource<Country> Source { get; }
/// <summary>
/// Gets the maximum population of all the countries.
/// </summary>
public int MaxPopulation { get; }
}