none
Populate WPF ListBox with ListBoxItem throught runspace

    Question

  • Hi guys, 

    I have created a UI with WPF. Everything works fine but UI freeze... So I try to use runspace.

    My UI is composed with a listbox use to show to the user what's going on. I create a custom function wich write in listbox with different color in fonction of the message type (error, warning, succes,...) : 

    function display ()
    {
        param([string] $text, [switch]$error, [switch]$succes,[switch]$warning, [switch]$title)  
        $item = New-Object System.Windows.Controls.ListBoxItem
        if ($error){
            $item.Content = "$text"
            $item.Foreground = "Red"   
        }  
        elseif ($succes)
        { 
            $item.Content = "$text"
            $item.Foreground = "Green"
        }
        elseif ($warning)
        {
            $item.Content = "$text"
            $item.Foreground = "Orange"
        }
        elseif ($title) 
        {         
            $item.Content = "$text"
            $item.Foreground = "Black"
            $item.Background = "Gray"
        }
        else 
        {      
            $item.Content = "$text"
        }
        uiHash.window_Main.Dispatcher.Invoke([action]{
            $uiHash.listBoxStatus.Items.Add($item)
            $lastIndex = $uiHash.listBoxStatus.Items.Count - 1
            $uiHash.listBoxStatus.SelectedIndex = $lastIndex    
        })
    }

    This function work great without using runspace but when I use it, it seems to "New-Object System.Windows.Controls.ListBoxItem" is not known in the runspace because if I create object out of the runspace and put it in parameter it's works... I try to load diferent assembly but nothing works... 

    My main runspace is in PS V4.

    Here my code for the call in runspace : 

    $scriptBlock = {        
            C:\Temp\functions.ps1              
            display -text "runspace test"        
        }
        
        $runspace = [runspacefactory]::CreateRunspace()
        $runspace.ApartmentState = "STA"
        $runspace.ThreadOptions = "ReuseThread"    
        $runspace.Open()
        $runspace.SessionStateProxy.SetVariable("uiHash",$uiHash)    
        
        $powershell = [powershell]::Create().AddScript($scriptBlock)
        $powershell.Runspace = runspace    
        $powershell = $powershell.BeginInvoke()

    The lastItem selection works like this but not the Add($item)... Anyone know how can I do that ? 

    Thanks for help

    Friday, March 31, 2017 2:06 PM

Answers

  • Your exemple doesn't work to me. 

    It's tell me "Method invocation failed because [System.Windows.Media.SolidColorBrush]does not ontain a mathod anmed 'New'"

    And except mistake from me, I don't see any call to runspace in your exemple...

    Anyway I found the solution, I built my object ListBoxItem outside of the Dispatcher.Invoke script block... So when I try to add item, it was unknow and do nothing. And because i'm in runspace, I don't have any error message...

    Thanks for help

    • Marked as answer by max8313 Friday, April 21, 2017 6:10 AM
    Friday, April 21, 2017 6:10 AM

All replies

  • That is because it is not a WPF class.  Just add items to the ListBox.

    Do not use both () and "param".


    \_(ツ)_/

    Friday, March 31, 2017 5:13 PM
  • I don't understand your answer... can you post the code with the correction you suggest? Thanks
    Saturday, April 01, 2017 12:09 PM
  • $listbox.Items.Add('some value')


    \_(ツ)_/

    Saturday, April 01, 2017 12:40 PM
  • Yes that work but I want to write in listbox with color, and that just display text without formating. 

    It's for this reason I use an objet with Foreground attribute. Maybe the object type I use isn't the better one for this but what can I use ? 

    Monday, April 03, 2017 7:33 AM
  • $i = $listbox.Items.Add('text')
    $item = $listbox.Items[$i]
    $item.ForeGround = 'Red'


    \_(ツ)_/



    • Edited by jrv Monday, April 03, 2017 8:33 AM
    Monday, April 03, 2017 8:20 AM
  • add-type -AssemblyName PresentationFramework
    $item = [System.Windows.Controls.ListBoxItem]::New()
    $item.Content = 'Text'
    $item.Foreground = 'Red'
    


    \_(ツ)_/

    Monday, April 03, 2017 8:27 AM
  • Hi, 

    Sorry for the return delay.

    I tried both of your propositions but anyone work... It's write text in my list box but it's stay black instead of Red I want... 

    Monday, April 10, 2017 12:17 PM
  • Hi, 

    No one has an idea for my problem ? 

    Thursday, April 20, 2017 12:01 PM
  • My code works for me.  Post a simple example of your WPF.  A form with just a listbox that you are having trouble with along with the code you are suing to update it.

    \_(ツ)_/

    Thursday, April 20, 2017 6:14 PM
  • Here is a quick example of how to do tis.  Run it at a prompt and you will see that is works as expected:

    Add-Type -Assembly 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 = "50" 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'
    $itm.Foreground = 'red'
    $ListBox.Items.Add($itm)
    $Window.ShowDialog()


    \_(ツ)_/


    • Edited by jrv Friday, April 21, 2017 5:08 AM
    Friday, April 21, 2017 12:40 AM
  • This may give you a better idea of what is happening with LB items constructed at runtime.

    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()


    \_(ツ)_/



    • Edited by jrv Saturday, April 22, 2017 9:46 PM
    Friday, April 21, 2017 5:07 AM
  • Your exemple doesn't work to me. 

    It's tell me "Method invocation failed because [System.Windows.Media.SolidColorBrush]does not ontain a mathod anmed 'New'"

    And except mistake from me, I don't see any call to runspace in your exemple...

    Anyway I found the solution, I built my object ListBoxItem outside of the Dispatcher.Invoke script block... So when I try to add item, it was unknow and do nothing. And because i'm in runspace, I don't have any error message...

    Thanks for help

    • Marked as answer by max8313 Friday, April 21, 2017 6:10 AM
    Friday, April 21, 2017 6:10 AM
  • As I posted - it is i possible to guess at what you are doing from your posts. 

    Your errors are because you are using an old version of PowerShell.

    Change:

    [System.Windows.Media.SolidColorBrush]::New()
    to
    New-Object System.Windows.Media.SolidColorBrush()

    I am sorry you are having  such a painful experience with this.  Even experienced programmers have issues with WPF.

    'runspaces" have little to do with your issues. 


    \_(ツ)_/


    • Edited by jrv Friday, April 21, 2017 6:34 AM
    Friday, April 21, 2017 6:32 AM
  • I understand that my post is light but the program I write is for administrative use of SCCM with confidential data, I can't post all my code here and it make over 2000 line... not easy to read it in here.

    I'm working on 2012 Server with native PS V4, this is not enought ? 

    First time i'm experiencing WPF and I use UserControl, Runspace, ... I'm crazy :) 

    Runspace just make troubleshooting harder 

    Thanks for your help 

    Friday, April 21, 2017 7:15 AM
  • In a technical forum it is necessary to post at least enough code to demonstrate you issue.  What you posted does not show how you created the ListBox or how you were accessing it.

    When using a delegate you need to be sire the variables exist in the same context as the event is going to run. Normally we define all event code inside of the runspace.

    Most of your issues are going to come because you lack any dorms design experience.  This will slow you down and lead you down blind alleys.  I don't recommend using WPF with PowerShell if you are not an experienced programmer. All forms can be built using standard Windows forms.  It is easier an much easier to debug.


    \_(ツ)_/

    Friday, April 21, 2017 7:21 AM
  • I agree that for being understood I need to provide enought code but you just tell me that now, 20 days after my first post... If you had tell me before I were posted other piece of code.

    And I agree for the use of WPF too but now I build interface with it I don't want to recreate everything with WinForm.  Next time I do that ;)

    Friday, April 21, 2017 7:29 AM
  • I gave you the answer in my second post.  You failed to explain that you were not coding the remainder correctly and just kept complain that it didn't work.  I cannot guess and all tech should know that they need to ask clear questions with useful examples.

    i the future try to be clear and you will get a better and faster answer.


    \_(ツ)_/

    Friday, April 21, 2017 7:40 AM