Private/WhatsUpGold/_WhatsUpGoldUIEvents.ps1

function _WhatsUpGoldUIEvents {
    #region General UI
    $WhatsUpGold.Add_SourceInitialized({
            _OnWhatsUpGoldSourceInitialized
        })

    $WhatsUpGold.Add_Activated({
        if ($HomeWindow) {
            $HomeWindow_LoadingLabel.Visibility = 'Hidden'
        }
        })

    $WhatsUpGold.Add_Closing({
            if ($HomeWindow) {
                $_.Cancel = $true
                $WhatsUpGold.hide()
            }
            else {
                # $WhatsUpGold.close()
            }
        })
    #endregion

    $WhatsUpGold_LookupButton.Add_Click({
            # TODO add column to datagrid that is either a circle or change the row's background if has a down.
            $WhatsUpGold_LoadingLabel.Visibility = 'Visible'
            $Script:DevicesDataTable = New-Object System.Data.DataTable
            $Script:Devices = _GetWUGDevices -GroupID ($DeviceGroups | Where-Object name -EQ $WhatsUpGold_DeviceGroupComboBox.Text).id | Select-Object @{Name = 'Health'; Expression = { if ($_.totalActiveMonitorsDown -gt 0) { 'Unhealthy'} else { 'Healthy'} } }, @{Name = 'Host Name'; Expression = { $_.hostName } }, @{Name = 'Name'; Expression = { $_.name } }, @{Name = 'IP Address'; Expression = { $_.networkAddress } }, @{Name = 'Operating System'; Expression = { $_.os } }, @{Name = 'Total Monitors'; Expression = { $_.totalActiveMonitors } }, @{Name = 'Down Monitors'; Expression = { $_.totalActiveMonitorsDown } }, @{Name = 'Up Monitors'; Expression = { $_.totalActiveMonitors - $_.totalActiveMonitorsDown } }, @{Name = 'Device ID'; Expression = { $_.id } }, @{Name = 'Notes'; Expression = { $_.notes } }
            $DevicesHeaders = [ordered]@{
                'Health' = [string]
                'Host Name' = [string]
                'IP Address' = [string]
                'Operating System' = [string]
                'Device ID' = [Int]
            }

            foreach ($Header in $DevicesHeaders.GetEnumerator()) {
                if (!($Header -like '*Monitors')) {
                    $WhatsUpGold_FilterComboBox.Items.Add($Header.Key)
                }
                $DevicesDataTable.Columns.Add($Header.Key,$Header.Value)
            }

            foreach ($Device in $Devices) {
                $Array = @()
                Foreach ($Header in $DevicesHeaders.GetEnumerator()) {
                    switch ($Header.Value) {
                        'string' {
                            $array += [string]$Device.$($Header.Key)
                        }
                        'int' {
                            $array += [int]$Device.$($Header.Key)
                        }
                    }
                }
                [void]$DevicesDataTable.Rows.Add($array)
            }
            $WhatsUpGold_DeviceListingDataGrid.DataContext = $DevicesDataTable.DefaultView
            $WhatsUpGold_LoadingLabel.Visibility = 'Hidden'
        })

    $WhatsUpGold_FilterTextBox.Add_TextChanged({
            $InputText = $WhatsUpGold_FilterTextBox.Text
            $FilterType = $WhatsUpGold_FilterComboBox.Text
            $filter = "[$FilterType] LIKE '%$InputText%'"
            Write-Verbose $filter
            $DevicesDataTable.DefaultView.RowFilter = $filter
            $WhatsUpGold_DeviceListingDataGrid.ItemsSource = $DevicesDataTable.DefaultView
        })

    $WhatsUpGold_DeviceListingDataGrid.Add_SelectionChanged({
            $WhatsUpGold_LoadingLabel.Visibility = 'Visible'
            # TODO account for selecteditem being $null

            $SelectedItem = $WhatsUpGold_DeviceListingDataGrid.SelectedItem
            if ($SelectedItem) {
            $SelectedDevice = $Devices | Where-Object 'Device ID' -EQ $SelectedItem.'Device ID'
            Write-Verbose ($SelectedDevice | ConvertTo-Json)
            $WhatsUpGold_NameTextBox.Text = $SelectedDevice.Name
            $WhatsUpGold_HostNameTextBox.Text = $SelectedDevice.'Host Name'
            $WhatsUpGold_IPAddressTextBox.Text = $SelectedDevice.'IP Address'
            $WhatsUpGold_OSTextBox.Text = $SelectedDevice.'Operating System'
            $WhatsUpGold_NotesTextBox.Text = $SelectedDevice.'Notes'

            if ($($SelectedDevice.'Down Monitors') -eq '0') {
                # All up
                $WhatsUpGold_StatusEllipse.Fill = '#FF64DD17'
                $WhatsUpGold_DownMonitorsTabItem.Visibility = 'Collapsed'
                $WhatsUpGold_DownMonitorTextBox.Text = ''
            }
            else {
                # Down Monitor
                $WhatsUpGold_StatusEllipse.Fill = '#FFFF6666'
                $WhatsUpGold_DownMonitorsTabItem.Visibility = 'Visible'
                # TODO if selection changes to null, account for that
                # TODO make this prettier
                $WhatsUpGold_DownMonitorTextBox.Text = (_GetWUGDevice -DeviceID $($SelectedDevice.'Device ID') -View card | Select-Object -ExpandProperty downactivemonitors | Out-String).trim()
            }
            #endregion

            #region Active Monitors
            $DeviceMonitors = _GetWUGDeviceMonitors -DeviceId $($SelectedDevice.'Device ID') -View Status -Type Active | Select-Object @{Name='Monitor Type Name';Expression={$_.monitorTypeName}}, @{Name='Status';Expression={$_.status}}, @{Name='Comment';Expression={$_.active.comment}}, @{Name='Description';Expression={$_.description}}

            # TODO how to sort these in a desired order
            # $DeviceMonitorHeaders = $DeviceMonitors | Get-Member | Where-Object membertype -EQ noteproperty | Select-Object -ExpandProperty name
            $DeviceMonitorHeaders = @(
                'Monitor Type Name'
                'Status'
                'Comment'
                'Description'
            )

            $DeviceMonitorDataTable = New-Object System.Data.DataTable
            $DeviceMonitorDataTable.Columns.AddRange($DeviceMonitorHeaders)
            foreach ($Monitors in $DeviceMonitors) {
                $Array = @()
                Foreach ($Header in $DeviceMonitorHeaders) {
                    $array += $Monitors.$Header
                }
                $DeviceMonitorDataTable.Rows.Add($array)
            }
            $WhatsUpGold_ActiveMonitorDataGrid.ItemsSource = $DeviceMonitorDataTable.DefaultView
            #endregion
            }

            $WhatsUpGold_LoadingLabel.Visibility = 'Hidden'
        })

    $WhatsUpGold_DropdownButton.Add_Click({
            # This uses the Marlett font.
            $UpArrow = '5'
            $DownArrow = '6'
            switch ($WhatsUpGold_DropdownButton.Content) {
                $UpArrow {
                    $WhatsUpGold_FilterStackPanel.Visibility = 'Collapsed'
                    $WhatsUpGold_DropdownButton.Content = $DownArrow
                }
                $DownArrow {
                    $WhatsUpGold_FilterStackPanel.Visibility = 'Visible'
                    $WhatsUpGold_DropdownButton.Content = $UpArrow
                }
                Default {
                }
            }
        })
}