WPF PowerShell Click on column name to sort

PowerShell code:


# Input Server
$serverSelected = $ServerNameTextbox.Text

$errMsgList = @()

# See if the server is online
if (-not(Test-Connection -ComputerName $serverSelected -Count 1 -Quiet))
$errMsgList += "Server -> [$serverSelected] does not exist or is not online"
$WindowsServicesListView.Items.Clear() # Clear the ListView

# Target servers
$services = Get-WmiObject win32_service -ComputerName $serverSelected

# List the services
foreach ($service in $services)

# Error
if ($errMsgList.Count -gt 0)
{   # Input is not valid
DisplayErrorMsgBox -Message ($errMsgList | Out-String)


I am trying to use sort by clicking on the column header but I can’t seem to find solution

I was only able to find c# code…



Thank you Rob, but I need the code in PowerShell syntax…

Form object control is in the hands of the form code, not PS.


Otherwise, understand that PS, extract data input or puts data into form elements, it does not control the form elements themselves.

If you want PS code to do this, then it has to clear / refresh the form element on that click event that calls your PS code that gets the data, pre-sorted then send back to the form element. This of course this sort of thing results in element flicker, meaning, the clear and replace is obvious, vs just sort in place via form code, which Rob is pointing to.