Share via


WPF / PowerShell ListBox : Change foreground color of item if it matches *expression*

Question

Saturday, April 22, 2017 9:21 PM

Hello !

I am trying to change the background color of items in ListBox depending on their content.

I tried this :

<ListBox x:Name="listBox_advList" HorizontalAlignment="Left" Height="277" Margin="10,38,0,0" VerticalAlignment="Top" Width="491">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <Trigger Property="Content" Value="GIE">
                        <Setter Property="ListBoxItem.Background" Value="LightGreen" />
                    </Trigger>
                    <Trigger Property="Content" Value="Console Admin Tools">
                        <Setter Property="ListBoxItem.Background" Value="Orange" />
                    </Trigger>                               
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>
        </ListBox>

I would like to change the color of each item to green or orange, if they match 'success' or 'failure'.

In other words, is there some kind of -match or -like applying to triggers that I could use, so that I can change the color of the item if it contains 'success' or 'failure' ?

Here is the rest of the code :

$WPF_button_getAdv.add_Click({

# Clear listBox
$WPF_listBox_advList.Items.Clear()
# Test connection
if (Test-Connection $($WPF_textBox_compName.Text) -Count 1 -ErrorAction SilentlyContinue)
    {
    # Get ADV
    $advertisement = Get-WmiObject -Query "SELECT * FROM CCM_Softwaredistribution" -Namespace "root\CCM\Policy\Machine\ActualConfig" -Computer $($WPF_textBox_compName.Text) -Authentication PacketPrivacy -Impersonation Impersonate
    # Sort ADV : Group by ADVID -> Foreach Object in the Group, select LAST element
    $global:sortAdv = $advertisement | Group-Object ADV_AdvertisementID | ForEach-Object { $_.Group | Select-Object -Last 1 }
    # Get Status
    $advStatus = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History" -Recurse | ForEach-Object { Get-ItemProperty -Path  $_.PsPath }
    foreach ($item in $global:sortAdv)
        {
        foreach ($value in $advStatus)
            {
            if ($value.PSPath -match $item.PKG_PackageID)
                {
                [array]$listBoxInput += "$($value._State)" + " - $($item.PKG_PackageID)" + " - $($item.PKG_Name)"
                }
            }
        }

    [array]$listBoxInput | ForEach-Object {
    $WPF_listBox_advList.AddText($_)
    }

    }
else { $WPF_textBox_compName.Text = 'Ping KO' }

})

What I want to do is change the color of each item depending on the value of $value._State. If it contains SUCCESS, item in the listBox should be green, if it contains FAILURE it should be orange / red...

Thanks a lot guys :)

All replies (4)

Saturday, April 22, 2017 9:35 PM ✅Answered

To change the colors you need to create the items.

Here is an example of how to colorize ListBox items.

Add-Type -AssemblyName presentationframework
[xml]$xaml = @'
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window" Title="Progress..." WindowStartupLocation = "CenterScreen"
    Width = "400" Height = "165" ShowInTaskbar = "True">
    <Grid>
        <ListBox x:Name="ListBox" Height = "150" Width = "365" HorizontalAlignment="Left" VerticalAlignment="Top"  Margin = "10,35,0,0"/>
    </Grid>
</Window>
'@
    
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$Window = [Windows.Markup.XamlReader]::Load($reader)
    
$ListBox = $Window.FindName("ListBox")

$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test red'
$itm.Foreground = 'red'
$ListBox.Items.Add($itm)

$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test green'
$itm.Foreground = 'green'
$ListBox.Items.Add($itm)

$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test blue'
$itm.Foreground = 'blue'
$ListBox.Items.Add($itm)

$Window.ShowDialog()

\(ツ)_/


Saturday, April 22, 2017 10:36 PM

Thanks, I'm giving it a try right now, but it does not work on my laptop.

Is it using PS5 features ?

[System.Windows.Media.SolidColorBrush]::new('blue')InvalidOperation / RuntimeExceptionMethodNotFound

Saturday, April 22, 2017 10:38 PM

Copy it again.

\(ツ)_/


Saturday, April 22, 2017 10:51 PM

It works exactly as expected, I love you ! :)

Thank you so much ! \o/