Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PowerShell WPF XAML DataGrid control databinding datacontext example - Part 3

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

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 = @"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataGrid with Data Binding and DataTemplate Example" Height="300" Width="400">
    
        {Binding}" AutoGenerateColumns="False">
            
                Header="Name" Binding="{Binding Name}" Width="*"/>
                Header="Age">
                    
                        
                            Binding Age}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        
                    
                    
                        
                            Binding Age, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        
                    
                
            
        
    

"@

# 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 = @"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataGrid with Data Binding and DataTemplate Example" Height="300" Width="400">
    
        Binding}" AutoGenerateColumns="False">
            
                Header="Name" Binding="{Binding Name}" Width="*"/>
                Header="Age">
                    
                        
                            Binding Age}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        
                    
                
            
        
    

"@

# 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



This post first appeared on Tales From Real IT System Administrators World And Non-production Environment, please read the originial post: here

Share the post

PowerShell WPF XAML DataGrid control databinding datacontext example - Part 3

×

Subscribe to Tales From Real It System Administrators World And Non-production Environment

Get updates delivered right to your inbox!

Thank you for your subscription

×