|
| 1 | +## Getting Started |
| 2 | + |
| 3 | +### 1. Create a New Uno Project |
| 4 | + |
| 5 | +If you don't already have a Uno project, create one in Visual Studio. |
| 6 | + |
| 7 | +### 2: Install NuGet Package |
| 8 | +Install [WinUI.TableView](https://www.nuget.org/packages/WinUI.TableView) NuGet package to your app with your preferred method. Here is the one using NuGet Package Manager: |
| 9 | + |
| 10 | +```bash |
| 11 | +Install-Package WinUI.TableView |
| 12 | +``` |
| 13 | +### 3. Add `WinUI.TableView` to Your XAML |
| 14 | + |
| 15 | +In your `MainPage.xaml`, add the `WinUI.TableView` control: |
| 16 | + |
| 17 | +```xml |
| 18 | +<Page x:Class="UnoApp1.MainPage" |
| 19 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 20 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 21 | + xmlns:tv="using:WinUI.TableView" |
| 22 | + Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> |
| 23 | + <Grid Margin="40"> |
| 24 | + <tv:TableView AutoGenerateColumns="False" |
| 25 | + ItemsSource="{x:Bind Items}"> |
| 26 | + <tv:TableView.Columns> |
| 27 | + <tv:TableViewTextColumn Header="Name" Width="250" Binding="{Binding Name}" /> |
| 28 | + <tv:TableViewNumberColumn Header="Price" Width="100" Binding="{Binding Price}" /> |
| 29 | + <tv:TableViewNumberColumn Header="Quantity" Width="100" Binding="{Binding Quantity}" /> |
| 30 | + <tv:TableViewDateColumn Header="Purchase Date" Width="140" Binding="{Binding PurchaseDate}" /> |
| 31 | + </tv:TableView.Columns> |
| 32 | + </tv:TableView> |
| 33 | + </Grid> |
| 34 | +</Page> |
| 35 | +``` |
| 36 | + |
| 37 | +### 4. Bind Data to `TableView` |
| 38 | + |
| 39 | +Create a simple Model class with properties to represent in TableView cells: |
| 40 | + |
| 41 | +```csharp |
| 42 | +public class Item : INotifyPropertyChanged |
| 43 | +{ |
| 44 | + private string? _name; |
| 45 | + private double _price; |
| 46 | + private int _quantity; |
| 47 | + private DateOnly _purchaseDate; |
| 48 | + |
| 49 | + public string? Name |
| 50 | + { |
| 51 | + get => _name; |
| 52 | + set |
| 53 | + { |
| 54 | + _name = value; |
| 55 | + OnPropertyChanged(nameof(Name)); |
| 56 | + } |
| 57 | + } |
| 58 | + public double Price |
| 59 | + { |
| 60 | + get => _price; |
| 61 | + set |
| 62 | + { |
| 63 | + _price = value; |
| 64 | + OnPropertyChanged(nameof(Price)); |
| 65 | + } |
| 66 | + } |
| 67 | + public int Quantity |
| 68 | + { |
| 69 | + get => _quantity; |
| 70 | + set |
| 71 | + { |
| 72 | + _quantity = value; |
| 73 | + OnPropertyChanged(nameof(Quantity)); |
| 74 | + } |
| 75 | + } |
| 76 | + public DateOnly PurchaseDate |
| 77 | + { |
| 78 | + get => _purchaseDate; |
| 79 | + set |
| 80 | + { |
| 81 | + _purchaseDate = value; |
| 82 | + OnPropertyChanged(nameof(PurchaseDate)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void OnPropertyChanged(string propertyName) |
| 87 | + { |
| 88 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 89 | + } |
| 90 | + |
| 91 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +In your `MainPage.xaml.cs`, set up the data context and bind data to the `TableView`: |
| 96 | + |
| 97 | +```csharp |
| 98 | +public sealed partial class MainPage : Page |
| 99 | +{ |
| 100 | + public IList<Item> Items { get; } |
| 101 | + |
| 102 | + public MainPage() |
| 103 | + { |
| 104 | + this.InitializeComponent(); |
| 105 | + |
| 106 | + Items = Enumerable.Range(1, 20).Select(i => GetRandomItem(i)).ToList(); |
| 107 | + } |
| 108 | + |
| 109 | + private static Item GetRandomItem(int i) |
| 110 | + { |
| 111 | + return new Item |
| 112 | + { |
| 113 | + Name = $"Random Item {i}", |
| 114 | + Price = Math.Round(Random.Shared.NextDouble() * 100, 2), |
| 115 | + Quantity = Random.Shared.Next(1, 100), |
| 116 | + PurchaseDate = DateOnly.FromDateTime(DateTime.Today.AddDays(Random.Shared.Next(-90, 90))) |
| 117 | + }; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### 5. Run Your Application |
| 123 | + |
| 124 | +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. |
| 125 | + |
| 126 | + |
0 commit comments