|
| 1 | +## Getting Started |
| 2 | + |
| 3 | +### 1. Create a New WinUI3 or Uno Project |
| 4 | + |
| 5 | +If you don't already have a WinUI 3 project or Uno prject, create one in Visual Studio. |
| 6 | + |
| 7 | +### 2: Install NuGet Package |
| 8 | +Inatall `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 `MainWindow.xaml`, add the `WinUI.TableView` control: |
| 16 | + |
| 17 | +```xaml |
| 18 | +<Window |
| 19 | + x:Class="App1.MainWindow" |
| 20 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 21 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 22 | + xmlns:local="using:App1" |
| 23 | + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
| 24 | + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
| 25 | + mc:Ignorable="d" |
| 26 | + xmlns:tv="using:WinUI.TableView"> |
| 27 | + |
| 28 | + <Grid> |
| 29 | + <tv:TableView x:Name="MyTableView" |
| 30 | + ItemsSource="{x:Bind ViewModel.Items}" /> |
| 31 | + </Grid> |
| 32 | +</Window> |
| 33 | +``` |
| 34 | + |
| 35 | +### 4. Bind Data to `TableView` |
| 36 | + |
| 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: |
| 52 | + |
| 53 | +```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 | +public class Item : INotifyPropertyChanged |
| 71 | +{ |
| 72 | + private string _name; |
| 73 | + private double _price; |
| 74 | + private int _quantity; |
| 75 | + |
| 76 | + public string Name |
| 77 | + { |
| 78 | + get => _name; |
| 79 | + set |
| 80 | + { |
| 81 | + _name = value; |
| 82 | + OnPropertyChanged(nameof(Name)); |
| 83 | + } |
| 84 | + } |
| 85 | + public double Price |
| 86 | + { |
| 87 | + get => _price; |
| 88 | + set |
| 89 | + { |
| 90 | + _price = value; |
| 91 | + OnPropertyChanged(nameof(Price)); |
| 92 | + } |
| 93 | + } |
| 94 | + public int Quantity |
| 95 | + { |
| 96 | + get => _quantity; |
| 97 | + set |
| 98 | + { |
| 99 | + _quantity = value; |
| 100 | + OnPropertyChanged(nameof(Quantity)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void OnPropertyChanged(string propertyName) |
| 105 | + { |
| 106 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 107 | + } |
| 108 | + |
| 109 | + public event PropertyChangedEventHandler PropertyChanged; |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### 5. Run Your Application |
| 114 | + |
| 115 | +Build and run your application. You should see the `TableView` populated with the rows and cells from your `ViewModel`. |
0 commit comments