Updating a listview - ObservableCollection?

May 8, 2012 at 10:24 AM
Edited May 8, 2012 at 10:25 AM

As an example, I have a task scheduler app, but I need the listview to udpate whenever the status of a task changes. This has similar uses for e.g. a process monitor, services manager etc. I think I can use an ObservableCollection for this, just not sure how to modify my code to use this.

Can anyone advise please? Here is my code:

import-module taskscheduler
window -show {
Grid -Name ScheduledTasksGrid -Rows Auto,1*,Auto {
   
     StackPanel -Orientation Horizontal {
    
        TextBox -Name computerName -Width 100 -Text "localhost" -Margin 5
        Button "Connect" -Width 100 -Height 25 -On_Click {
            $ScheduledTaskListView.ItemsSource = @(get-scheduledtask -name * -computername $computername.text)
        }
               Button "Start" -Width 100 -Height 25 -On_Click {
               get-scheduledtask -name $SelectedTaskName | start-task

               }
               
    }    
           
           ListView -Row 1 -Name ScheduledTaskListView -Background White -MaxHeight 500 -View {
        GridView -Columns {
            GridViewColumn -Header Name -DisplayMemberBinding Name
            GridViewColumn Status
            GridViewColumn LastRunTime
            GridViewColumn NextRunTime
        }
    } -On_SelectionChanged  {
     $SelectedTaskName = $ScheduledTaskListView.SelectedValue.Name
    $SelectedTaskStatus = $ScheduledTaskListView.SelectedValue.Status
     
    }
    }  
    }
Coordinator
May 11, 2012 at 8:56 PM

So sorry I let this sit for awhile, I've been out of the WPF world for a bit, and it took me this long to get my head back in the game and realize what you asked. I think the building blocks you need are already in a couple of other posts.  You're right about the ObservableCollection, there's a big example of how to use those here http://showui.codeplex.com/discussions/271347 already ...

The ObservableCollection will take care of when new things are added to the list, but will not take care of when a property of an item changes, because ObservableCollection ignores INotifyPropertyChanged -- so even if your objects actually try to notify us that a property (e.g. the task's .TaskStatus or something) changed, the notification won't make it out of the collection to the UI unless you bind on every item when they get added to the collection (probably more pain than it's worth). 

The simple fix is to .Refresh the ListView when you change something, as shown in this thread: http://showui.codeplex.com/discussions/270838  (note that you don't necessarily need the "Get-Job ... | Update-WPFJob" stuff, unless you need to update the UI from the console instead of an event handler in the UI.

May 13, 2012 at 7:14 PM

Hi Jakul

Actually I think what I want isn't possible (newbie programmer) - I wanted the listview which showed scheduled tasks to update based on the status of the task - e.g. if one stopped, it should show as stopped. So I wanted it to be constantly updated....is that possible without doing a manual refresh every second or something?

 

 

Coordinator
May 14, 2012 at 3:04 AM
It's possible only if you have objects that update themselves based on the status of the task(s), I'm not sure if the new tasks cmdlets in PS3 return live objects or not, but in PowerShell 2 (or in .Net, in general) I don't know of any scheduled tasks API that would return live data -- you'd have to loop.

We have a bunch of work in ShowUI around creating UI that re-runs a script to generate the data over and over: look at Get-PowerShellDataSource and Update-WPFJob for example.
--
Joel "Jaykul" Bennett
http://HuddledMasses.org
http://PowerShellGroup.org


On Sun, May 13, 2012 at 2:14 PM, hsclater <notifications@codeplex.com> wrote:

From: hsclater

Hi Jakul

Actually I think what I want isn't possible (newbie programmer) - I wanted the listview which showed scheduled tasks to update based on the status of the task - e.g. if one stopped, it should show as stopped. So I wanted it to be constantly updated....is that possible without doing a manual refresh every second or something?

Read the full discussion online.

To add a post to this discussion, reply to this email (showui@discussions.codeplex.com)

To start a new discussion for this project, email showui@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com