Private/WhatsUpGold/_WhatsUpGoldUIEvents.ps1

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

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

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

    $Script:DevicesDataTable = New-Object System.Data.DataTable

    $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: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 } }
            Write-Verbose ($Devices.count)
            # $DevicesHeaders = $Devices | Get-Member | Where-Object membertype -EQ noteproperty | Select-Object -ExpandProperty name
            $DevicesHeaders = @(
                'Health'
                'Host Name'
                'IP Address'
                'Operating System'
                'Total Monitors'
                'Down Monitors'
                'Up Monitors'
                'Device ID'
            )

            foreach ($Header in $DevicesHeaders) {
                $WhatsUpGold_FilterComboBox.Items.Add($Header)
            }

            $DevicesDataTable.Columns.AddRange($DevicesHeaders)
            foreach ($Device in $Devices) {
                $Array = @()
                Foreach ($Header in $DevicesHeaders) {
                    $array += $Device.$Header
                }
                [void]$DevicesDataTable.Rows.Add($array)
            }
            $WhatsUpGold_DeviceListingDataGrid.ItemsSource = $DevicesDataTable.DefaultView

            $WhatsUpGold_LoadingLabel.Visibility = 'Hidden'
        })

    $WhatsUpGold_FilterTextBox.Add_TextChanged({
            # TODO not working
            $InputText = $WhatsUpGold_FilterTextBox.Text
            $FilterType = $WhatsUpGold_FilterComboBox.Text
            $filter = "'$FilterType' LIKE '$InputText%'"
            $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
            Write-Verbose ($SelectedItem | ConvertTo-Json)

            $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 ($($SelectedItem.'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 $($SelectedItem.'Device ID') -View card | Select-Object -ExpandProperty downactivemonitors | Out-String).trim()
            }
            #endregion

            #region Active Monitors
            $DeviceMonitors = _GetWUGDeviceMonitors -DeviceId $($SelectedItem.'Device ID') -View Status -Type Active | Select-Object monitorTypeName, status

            # TODO how to sort these in a desired order
            $DeviceMonitorHeaders = $DeviceMonitors | Get-Member | Where-Object membertype -EQ noteproperty | Select-Object -ExpandProperty name

            $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 {
                }
            }
        })
}