WPF PowerShell Click on column name to sort

Welcome Forums General PowerShell Q&A WPF PowerShell Click on column name to sort

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #129969
    Jay

    Participant
    Points: 16
    Rank: Member

    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

  • #130380

    Participant
    Points: 428
    Helping Hand
    Rank: Contributor
    • #130407
      Jay

      Participant
      Points: 16
      Rank: Member

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

  • #130482

    Participant
    Points: 502
    Helping Hand
    Rank: Major Contributor

    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.

You must be logged in to reply to this topic.