Earlier I wrote two articles regarding PowerShell WPF for DataBinding examples. In this article I will be using DataBinding to use with DataGrid control.
PowerShell WPF XAML simple data binding datacontext example - Part 1
PowerShell WPF XAML control to control data binding datacontext example - Part 2
PowerShell WPF XAML DataGrid control databinding datacontext example - Part 3
PowerShell WPF XAML multibinding datacontext template example - Part 4
Although I don't need to define any binding if I am planning to use simply PowerShell object directly into Datagrid as ItemSource. But with binding I get more power to manipulate data and below is the sample code for that.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
Add-Type -AssemblyName PresentationFramework # Define data items in Json $list = @' [ { "Name": "John Doe", "Age": "30" }, { "Name": "Jane Smith", "Age": "25" }, { "Name": "Bob Johnson", "Age": "40" } ] '@ # Create sample data $people = $list | ConvertFrom-Json # Create XAML markup for the WPF application $xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DataGrid with Data Binding and DataTemplate Example" Height="300" Width="400"> <Grid> <DataGrid x:Name='DataGrid' ItemsSource="{Binding}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/> <DataGridTemplateColumn Header="Age"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Age}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window> "@ # Create a WPF XML reader and load the XAML markup $reader = [System.Xml.XmlReader]::Create([System.IO.StringReader] $xaml) $window = [Windows.Markup.XamlReader]::Load($reader) # Set the data context for the DataGrid $dataContext = $window.FindName('DataGrid') $dataContext.ItemsSource = $people # Show the window $window.ShowDialog() | Out-Null <# Add-Type -AssemblyName PresentationFramework # Define a class for the data items class Person { [string]$Name [int]$Age Person([string]$name, [int]$age) { $this.Name = $name $this.Age = $age } } # Create sample data $people = @( [Person]::new('John Doe', 30), [Person]::new('Jane Smith', 25), [Person]::new('Bob Johnson', 40) ) # Create a data source (ObservableCollection of PSObjects) #$people = New-Object System.Collections.ObjectModel.ObservableCollection[PSObject] # Add data to the data source #$people.Add([PSCustomObject]@{ Name = "John Doe"; Age = 30 }) #$people.Add([PSCustomObject]@{ Name = "Jane Smith"; Age = 25 }) # Create XAML markup for the WPF application $xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DataGrid with Data Binding and DataTemplate Example" Height="300" Width="400"> <Grid> <DataGrid x:Name='DataGrid' ItemsSource="{Binding}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/> <DataGridTemplateColumn Header="Age"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Age}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window> "@ # Create a WPF XML reader and load the XAML markup $reader = [System.Xml.XmlReader]::Create([System.IO.StringReader] $xaml) $window = [Windows.Markup.XamlReader]::Load($reader) # Set the data context for the DataGrid $dataContext = $window.FindName('DataGrid') $dataContext.ItemsSource = $people # Show the window $window.ShowDialog() | Out-Null #> |
Download this code PowerShell_WPF_DataGrid_binding here, It is also available on github.com/janviudapi.
Useful Articles
COOL POWERSHELL FREE ONLINE GUI GENERATOR TOOL, POSHGUI
Generate random password GUI using powershell
Securely Encrypt and Decrypt password with PowerShell GUI tool
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial
Part 3: Create shorter Microsoft Powershell WPF automated clean script
Powershell WPF Themes guide step by step
Part 2: Powershell WPF Themes guide step by step
Part 3: Powershell wpf MahApps.Metro theme step by step
Powershell WPF MahApps.Metro update theme library
Powershell WPF custom Image based ProgressBar
Powershell WPF Charts dashboard demo