WPF PowerShell Click on column name to sort

PowerShell code:

$ServerSelectButton.Add_click({

# 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"
}
else
{
$WindowsServicesListView.Items.Clear() # Clear the ListView

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

# List the services
foreach ($service in $services)
{
$WindowsServicesListView.Items.Add([pscustomobject]@{SVCName="$($service.name)";Status="$($service.state)";StartupType="$($service.startmode)";Logon="$($service.StartName)"})
}
}

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

})

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…

 

https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-sort-a-gridview-column-when-a-header-is-clicked

https://stackoverflow.com/questions/5830033/how-can-i-add-sorting-by-clicking-on-the-column-header-in-this-gridview
http://www.thomaslevesque.com/2009/08/04/wpf-automatically-sort-a-gridview-continued/

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

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

https://blog.thoughtstuff.co.uk/2012/01/wpf-sorting-gridview-columns-by-clicking-headers

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.