Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell WPF XAML simple data binding datacontext example - Part 1

Windows Presentation Foundation (WPF) provides a simple and consistent way for applications to present and interact with data by binding elements to data from different kinds of data sources in the form of .NET objects and XML.

Here in this example I will show how to create simple WPF binding using PowerShell data object. In the script from line number from 4 to 12, I have created sample data object in PowerShell using classes. They have two properties Name and Age. For the data object instead of class you alternatively you can use json or psobject in PowerShell. 

To use properties mentioned in PowerShell data object in XAML form I have added binding information reference in TextBox controls, on line number from 20 to 23.

On line number 34, I am using PowerShell data object in Window DataContext. Which will show up on WPF xaml controls.

 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
Add-Type -AssemblyName PresentationFramework

# Define a .NET class representing your data object
class Person {
    [string] $Name
    [int] $Age
}

# Create an instance of the Person class
$person = [Person]::new()
$person.Name = "John Wick"
$person.Age = 60

# Create XAML markup for the WPF application
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="Data Binding Example" Height="200" Width="300">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Name:"/>
            <TextBox Text="{Binding Name, Mode=TwoWay}"/>
            <TextBlock Text="Age:"/>
            <TextBox Text="{Binding Age, Mode=TwoWay}"/>
        </StackPanel>
    </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 of the window to the person object
$window.DataContext = $person

# Show the window
$window.ShowDialog() | Out-Null

Related Articles
PowerShell WPF XAML simple data binding datacontext example - Part 1
PowerShell WPF XAML control to control data binding datacontext example - Part 2

This is screenshot of the PowerShell GUI. Information is taken from DataContext

Microsoft WPF Powershell xaml datacontext binding infromation example sample powershell classes data object .net class.png

In this below example, I have converted above WPF XAML form to native PoweShell .net object System.Windows.Window. Result is same.

Download these script here PowerShell_WPF_DataBinding or they are available on github.com/janviudapi.

 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
Add-Type -AssemblyName PresentationFramework

# Define a .NET class representing your data object
class Person {
    [string] $Name
    [int] $Age
}

# Create an instance of the Person class
$person = [Person]::new()
$person.Name = "John Doe"
$person.Age = 30

# Create a new Window object
$window = New-Object System.Windows.Window
$window.Title = "Data Binding Example"
$window.Height = 200
$window.Width = 300

# Create a Grid to hold the controls
$grid = New-Object System.Windows.Controls.Grid
$window.Content = $grid

# Create a StackPanel to hold the text blocks and text boxes
$stackPanel = New-Object System.Windows.Controls.StackPanel
$stackPanel.HorizontalAlignment = "Center"
$stackPanel.VerticalAlignment = "Center"
$grid.Children.Add($stackPanel)

# Create a TextBlock for the name
$txtNameLabel = New-Object System.Windows.Controls.TextBlock
$txtNameLabel.Text = "Name:"
$stackPanel.Children.Add($txtNameLabel)

# Create a TextBox for the name and bind it to the person's Name property
$txtName = New-Object System.Windows.Controls.TextBox
$txtName.SetBinding([System.Windows.Controls.TextBox]::TextProperty, "Name")
$txtName.DataContext = $person
$stackPanel.Children.Add($txtName)

# Create a TextBlock for the age
$txtAgeLabel = New-Object System.Windows.Controls.TextBlock
$txtAgeLabel.Text = "Age:"
$stackPanel.Children.Add($txtAgeLabel)

# Create a TextBox for the age and bind it to the person's Age property
$txtAge = New-Object System.Windows.Controls.TextBox
$txtAge.SetBinding([System.Windows.Controls.TextBox]::TextProperty, "Age")
$txtAge.DataContext = $person
[void]$stackPanel.Children.Add($txtAge)

# Show the window
$window.ShowDialog() | Out-Null

Useful Articles
Powershell GUI encode decode images
Powershell GUI format text on TextBox and RichTextBox
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out
Powshell GUI Date and Time converter tool
Powershell wpf gui, date and time converter select timezone get-date, timzone standard time ps1 script utc time, now.png

Powershell WPF GUI: ToolBox control Textbox watermark placeholder demo
Powershell WPF GUI: LinkLabel hyperlink demo
PowerShell WPF before and after image slider

Go Back

Comment

Blog Search

Page Views

11379991

Follow me on Blogarama