1
1
## Getting Started
2
2
3
- ### 1. Create a New WinUI3 or Uno Project
3
+ ### 1. Create a New WinUI3
4
4
5
- If you don't already have a WinUI 3 project or Uno prject, create one in Visual Studio.
5
+ If you don't already have a WinUI 3 project prject, create one in Visual Studio.
6
6
7
7
### 2: Install NuGet Package
8
8
Inatall ` WinUI.TableView ` NuGet package to your app with your preferred method. Here is the one using NuGet Package Manager:
@@ -14,66 +14,40 @@ Install-Package WinUI.TableView
14
14
15
15
In your ` MainWindow.xaml ` , add the ` WinUI.TableView ` control:
16
16
17
- ``` xaml
17
+ ``` xml
18
18
<Window
19
19
x : Class =" App1.MainWindow"
20
20
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
21
21
xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml"
22
- xmlns : local =" using:App1"
23
22
xmlns : d =" http://schemas.microsoft.com/expression/blend/2008"
24
23
xmlns : mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
24
+ xmlns : tv =" using:WinUI.TableView"
25
25
mc : Ignorable =" d"
26
- xmlns : tv =" using:WinUI.TableView" >
26
+ Title =" App1" >
27
+
28
+ <Window .SystemBackdrop>
29
+ <MicaBackdrop />
30
+ </Window .SystemBackdrop>
27
31
28
32
<Grid >
29
- <tv : TableView x : Name =" MyTableView"
30
- ItemsSource =" {x:Bind ViewModel.Items}" />
33
+ <tv : TableView ItemsSource =" {x:Bind Items}" />
31
34
</Grid >
32
35
</Window >
33
36
```
34
37
35
38
### 4. Bind Data to ` TableView `
36
39
37
- In your ` MainPage.xaml.cs ` , set up the data context and bind data to the ` TableView ` :
38
-
39
- ``` csharp
40
- public sealed partial class MainWindow : Window
41
- {
42
- public MainViewModel ViewModel { get ; } = new MainViewModel ();
43
-
44
- public MainWindow ()
45
- {
46
- this .InitializeComponent ();
47
- }
48
- }
49
- ```
50
-
51
- Create a simple ` MainViewModel ` with a collection of items to display:
40
+ Create a simple Model class with properties to represent in TableView cells:
52
41
53
42
``` csharp
54
- public class MainViewModel
55
- {
56
- public ObservableCollection <Item > Items { get ; set ; }
57
-
58
- public MainViewModel ()
59
- {
60
- Items = new ObservableCollection <Item >
61
- {
62
- new Item { Name = " Item 1" , Price = 10 . 0 , Quantity = 1 },
63
- new Item { Name = " Item 2" , Price = 15 . 0 , Quantity = 2 },
64
- new Item { Name = " Item 3" , Price = 20 . 0 , Quantity = 3 },
65
- // Add more items here
66
- };
67
- }
68
- }
69
-
70
43
public class Item : INotifyPropertyChanged
71
44
{
72
- private string _name ;
45
+ private string ? _name ;
73
46
private double _price ;
74
47
private int _quantity ;
48
+ private DateOnly _purchaseDate ;
75
49
76
- public string Name
50
+ public string ? Name
77
51
{
78
52
get => _name ;
79
53
set
@@ -100,16 +74,56 @@ public class Item : INotifyPropertyChanged
100
74
OnPropertyChanged (nameof (Quantity ));
101
75
}
102
76
}
77
+ public DateOnly PurchaseDate
78
+ {
79
+ get => _purchaseDate ;
80
+ set
81
+ {
82
+ _purchaseDate = value ;
83
+ OnPropertyChanged (nameof (PurchaseDate ));
84
+ }
85
+ }
103
86
104
87
private void OnPropertyChanged (string propertyName )
105
88
{
106
89
PropertyChanged ? .Invoke (this , new PropertyChangedEventArgs (propertyName ));
107
90
}
108
91
109
- public event PropertyChangedEventHandler PropertyChanged ;
92
+ public event PropertyChangedEventHandler ? PropertyChanged ;
93
+ }
94
+ ```
95
+
96
+ In your ` MainWindow.xaml.cs ` , set up the data context and bind data to the ` TableView ` :
97
+
98
+ ``` csharp
99
+ public sealed partial class MainWindow : Window
100
+ {
101
+ public IList Items { get ; }
102
+
103
+ public MainWindow ()
104
+ {
105
+ this .InitializeComponent ();
106
+
107
+ Items = Enumerable .Range (1 , 20 ).Select (i => GetRandomItem (i )).ToList ();
108
+ }
109
+
110
+ private static Item GetRandomItem (int i )
111
+ {
112
+ return new Item
113
+ {
114
+ Name = $" Random Item {i }" ,
115
+ Price = Math .Round (Random .Shared .NextDouble () * 100 , 2 ),
116
+ Quantity = Random .Shared .Next (1 , 100 ),
117
+ PurchaseDate = DateOnly .FromDateTime (DateTime .Today .AddDays (Random .Shared .Next (- 90 , 90 )))
118
+ };
119
+ }
110
120
}
111
121
```
112
122
113
123
### 5. Run Your Application
114
124
115
- Build and run your application. You should see the ` TableView ` populated with the rows and cells from your ` ViewModel ` .
125
+ Build and run your application. You should see the ` WinUI.TableView ` populated with the rows and cells from your ` Items ` collection. Here is the result by running the app on Desktop platform.
126
+
127
+
128
+ ![ image] ( https://github.com/user-attachments/assets/e00bffc7-19e0-40bd-bbda-07198d6bc60a )
129
+
0 commit comments