Menu

Virtual Geek

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

PowerShell WPF XAML control to control data binding datacontext example - Part 2

This is a part 2 of PowerShell WPF XAML simple data binding datacontext example - Part 1. Here I will configure control to control data binding context in WPF XAML. In this example I am using Slider and TextBox controls to show example. Here if I change Slider it should update its value on TextBox and updating TextBox text in number should move the Slider to that number entered in TextBox value.

All the code is on only WPF XAML 2 line numbers of 13 and 14. If you look closely I haven't defined any code in PowerShell. All the calculation is done inside WPF XAML only.

 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

#Written By: vcloud-lab.com 
#Auther: vjanvi

# 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="Slider and TextBox Data Binding Example" Height="200" Width="400">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Slider x:Name="sourceSlider" Value="50" Minimum="0" Maximum="100" Width="120" Margin="5"/>
            <TextBox Text="{Binding ElementName=sourceSlider, Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="5"/>
        </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)

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

<#
Add-Type -AssemblyName PresentationFramework

#Written By: vcloud-lab.com 
#Auther: vjanvi

# 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="Binding Example" Height="200" Width="300">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBox x:Name="sourceTextBox" Text="Hello" Width="120"/>
            <TextBox Text="{Binding ElementName=sourceTextBox, Path=Text, Mode=OneWay}" Width="120" IsReadOnly="True"/>
        </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)

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

Read more about WPF binding on: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-7.0

Below is the screenshot of the PowerShell WPF GUI after launch. When I move Slider it changes the according value in Textbox and if I manually enter number between 0 to 100 in TextBox, Slider is moved according to its value.

PowerShell WPF XAML data binding slider textbox control to control dataTemplate datacontext examples samples path value update trigger.png

Related Articles
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

I have converted above to WPF XAML example to show how it will work with Microsoft System.Windows.Forms .net object. 

Download this code PowerShell_WPF_Control_Binding here or it 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
 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
Add-Type -AssemblyName System.Windows.Forms

#Written By: vcloud-lab.com 
#Auther: vjanvi

# Create a form object
$form = New-Object System.Windows.Forms.Form
$form.Text = "Slider and TextBox Data Binding Example"
$form.Size = New-Object System.Drawing.Size(400, 200)

# Create a panel for the stack panel layout
$panel = New-Object System.Windows.Forms.Panel
$panel.Dock = [System.Windows.Forms.DockStyle]::Fill

# Create a stack panel
$stackPanel = New-Object System.Windows.Forms.FlowLayoutPanel
$stackPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
$stackPanel.FlowDirection = [System.Windows.Forms.FlowDirection]::TopDown
$stackPanel.WrapContents = $false
$stackPanel.Padding = New-Object System.Windows.Forms.Padding(10)

# Create a slider control
$slider = New-Object System.Windows.Forms.TrackBar
$slider.Name = "sourceSlider"
$slider.Minimum = 0
$slider.Maximum = 100
$slider.Width = 120
$slider.Value = $slider.Minimum + (($slider.Maximum - $slider.Minimum) / 2)
$slider.Margin = New-Object System.Windows.Forms.Padding(5)

# Create a textbox control
$textBox = New-Object System.Windows.Forms.TextBox
[void]$textBox.DataBindings.Add("Text", $slider, "Value", $true, "OnPropertyChanged")
$textBox.Margin = New-Object System.Windows.Forms.Padding(5)

# Add the controls to the stack panel
[void]$stackPanel.Controls.Add($slider)
[void]$stackPanel.Controls.Add($textBox)

# Add the stack panel to the panel
[void]$panel.Controls.Add($stackPanel)

# Add the panel to the form
[void]$form.Controls.Add($panel)

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

<#
Add-Type -AssemblyName PresentationFramework

#Written By: vcloud-lab.com 
#Auther: vjanvi

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

# Create a Grid object
$grid = New-Object System.Windows.Controls.Grid

# Create a StackPanel object
$stackPanel = New-Object System.Windows.Controls.StackPanel
$stackPanel.HorizontalAlignment = "Center"
$stackPanel.VerticalAlignment = "Center"

# Create a TextBox as the source control
$sourceTextBox = New-Object System.Windows.Controls.TextBox
$sourceTextBox.Name = "sourceTextBox"
$sourceTextBox.Text = "Hello"
$sourceTextBox.Width = 120

# Create a TextBox as the target control
$targetTextBox = New-Object System.Windows.Controls.TextBox
$targetTextBox.Text = $sourceTextBox.Text
$targetTextBox.Width = 120
$targetTextBox.IsReadOnly = $true

# Create a Binding object
$binding = New-Object System.Windows.Data.Binding
$binding.Source = $sourceTextBox
$binding.Path = New-Object System.Windows.PropertyPath("Text")
$binding.Mode = "OneWay"

# Apply the binding to the target TextBox
$targetTextBox.SetBinding([System.Windows.Controls.TextBox]::TextProperty, $binding)

# Add the controls to the StackPanel
[void]$stackPanel.Children.Add($sourceTextBox)
[void]$stackPanel.Children.Add($targetTextBox)

# Add the StackPanel to the Grid
[void]$grid.Children.Add($stackPanel)

# Set the Grid as the content of the Window
$window.Content = $grid

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

#>

Useful Information
Powershell: Change DNS IP addresses remotely on multiple computers using CIM & WMIPowershell: Change DNS IP addresses remotely on multiple computers using CIM & WMI
Installing, importing and using any module in powershell
Microsoft PowerShell: Check Windows license activation status
Find next available free drive letter using PowerShell
Copy Files with PowerShell Remoting WINRM Protocol
Powershell Find application window state minimized or maximized
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resolved issue with PowerShell - Trust relationship Rejoin computers in domain without restart

Go Back

Comment

Blog Search

Page Views

11189306

Follow me on Blogarama