Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Updating only visible rows in WPF ListView

Updating only visible rows in WPF ListView

Scheduled Pinned Locked Moved WPF
helpcsharpwpfannouncement
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pankaj Chamria
    wrote on last edited by
    #1

    I am using a GridView inside the WPF ListView control. On display of data I perform some validations and change color of some cells in each row accordingly. The problem is that there can be thousands of such rows in the ListView (bound to an observable collection)which leads to high CPU utilization. Is there a way that i can update only the visible rows in the listview. And as I scroll the listview down the next set of rows start getting updated.. This way the CPU utilization would be kept in check inspite of thousands of rows being updated in the observable collection.. Any help is appreciated. Thanks.

    Pankaj Chamria, Software Programmer.

    P 1 Reply Last reply
    0
    • P Pankaj Chamria

      I am using a GridView inside the WPF ListView control. On display of data I perform some validations and change color of some cells in each row accordingly. The problem is that there can be thousands of such rows in the ListView (bound to an observable collection)which leads to high CPU utilization. Is there a way that i can update only the visible rows in the listview. And as I scroll the listview down the next set of rows start getting updated.. This way the CPU utilization would be kept in check inspite of thousands of rows being updated in the observable collection.. Any help is appreciated. Thanks.

      Pankaj Chamria, Software Programmer.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I would use a VirtualizingStackPanel on the listview. This is an attached property, that is used like this:

      <ListBox VirtualizingStackPanel.IsVirtualizing = "True" ItemsSource="{Binding MySource}" ItemsTemplate="{Binding MyTemplate}" /<

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      My blog | My articles | MoXAML PowerToys

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        I would use a VirtualizingStackPanel on the listview. This is an attached property, that is used like this:

        <ListBox VirtualizingStackPanel.IsVirtualizing = "True" ItemsSource="{Binding MySource}" ItemsTemplate="{Binding MyTemplate}" /<

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        My blog | My articles | MoXAML PowerToys

        P Offline
        P Offline
        Pankaj Chamria
        wrote on last edited by
        #3

        Thanks for the reply. I tried your suggestion as shown below for my ListView for which the ItemsSource get assigned to an observable collection from code-behind. However it did not show any performance enhancement.. Can you check and see whether i am missing something. Also important to note is that the datasource is getting updated continuosly using a live data stream. And i am trying to reflect those changes in the ListView.

        <StackPanel Orientation="Vertical" x:Name="pnlMain">
        <ListView VirtualizingStackPanel.IsVirtualizing="True" x:Name="gridControl1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
        Width="Auto" Height="Auto" ScrollViewer.CanContentScroll="False"
        IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler"
        local:ItemsControlBehavior.AlternateItemContainerStyle="{DynamicResource alternateItemStyle}">
        <ListView.View>
        <GridView x:Name="view" ColumnHeaderContainerStyle="{DynamicResource MyHeaderStyle}">
        </GridView>
        </ListView.View>
        <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
        </ListView.ItemContainerStyle>

               </ListView>
        

        </StackPanel>

        Thanks!

        Pankaj Chamria, Software Programmer.

        P 1 Reply Last reply
        0
        • P Pankaj Chamria

          Thanks for the reply. I tried your suggestion as shown below for my ListView for which the ItemsSource get assigned to an observable collection from code-behind. However it did not show any performance enhancement.. Can you check and see whether i am missing something. Also important to note is that the datasource is getting updated continuosly using a live data stream. And i am trying to reflect those changes in the ListView.

          <StackPanel Orientation="Vertical" x:Name="pnlMain">
          <ListView VirtualizingStackPanel.IsVirtualizing="True" x:Name="gridControl1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
          Width="Auto" Height="Auto" ScrollViewer.CanContentScroll="False"
          IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Auto"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
          GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler"
          local:ItemsControlBehavior.AlternateItemContainerStyle="{DynamicResource alternateItemStyle}">
          <ListView.View>
          <GridView x:Name="view" ColumnHeaderContainerStyle="{DynamicResource MyHeaderStyle}">
          </GridView>
          </ListView.View>
          <ListView.ItemContainerStyle>
          <Style TargetType="ListViewItem">
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          </Style>
          </ListView.ItemContainerStyle>

                 </ListView>
          

          </StackPanel>

          Thanks!

          Pankaj Chamria, Software Programmer.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Is it your list that's being heavily hit? My suspicion here is that the ObservableCollection is actually the cause of your problem. Every time you add an item, it fires off the change notification, which results in the list being updated. To minimize the number of times it needs to update, I would consider adding the data in to the collection in reasonably large batches. I use a custom observable collection[^] to allow me to add large amounts of data.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          My blog | My articles | MoXAML PowerToys

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            Is it your list that's being heavily hit? My suspicion here is that the ObservableCollection is actually the cause of your problem. Every time you add an item, it fires off the change notification, which results in the list being updated. To minimize the number of times it needs to update, I would consider adding the data in to the collection in reasonably large batches. I use a custom observable collection[^] to allow me to add large amounts of data.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            My blog | My articles | MoXAML PowerToys

            P Offline
            P Offline
            Pankaj Chamria
            wrote on last edited by
            #5

            My problem is that once the data gets loaded in the observable collection for the first time, the values of properties of the objects in the collection are continuosly getting changed to the tune of 500-600 objects per second and each object has about 18 properties which get updated. The object itself is not getting overwritten/ added/ deleted. Its properties are just being modified. Also i observe that the Collection Change and Property Change events do not get called for such updates. It gets called only when the collection is first being created. With virtualization i was hoping that although 500 objects may get updated evry sec but the UI changes will be generated only for the visible objects. But that is not the case with my usage of observable collection. I guess due such large no of updates every second the UI sort of hangs, and CPU utilization goes 80-90%. Any suggestions what can i try further? Thanks!

            Pankaj Chamria, Software Programmer.

            P 1 Reply Last reply
            0
            • P Pankaj Chamria

              My problem is that once the data gets loaded in the observable collection for the first time, the values of properties of the objects in the collection are continuosly getting changed to the tune of 500-600 objects per second and each object has about 18 properties which get updated. The object itself is not getting overwritten/ added/ deleted. Its properties are just being modified. Also i observe that the Collection Change and Property Change events do not get called for such updates. It gets called only when the collection is first being created. With virtualization i was hoping that although 500 objects may get updated evry sec but the UI changes will be generated only for the visible objects. But that is not the case with my usage of observable collection. I guess due such large no of updates every second the UI sort of hangs, and CPU utilization goes 80-90%. Any suggestions what can i try further? Thanks!

              Pankaj Chamria, Software Programmer.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Wow. That sounds like a lot of notifications being fired there (I'm assuming here that you are using INotifyPropertyChanged here). You may want to put some of your processing onto a background thread.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              My blog | My articles | MoXAML PowerToys

              P 1 Reply Last reply
              0
              • P Pete OHanlon

                Wow. That sounds like a lot of notifications being fired there (I'm assuming here that you are using INotifyPropertyChanged here). You may want to put some of your processing onto a background thread.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                My blog | My articles | MoXAML PowerToys

                P Offline
                P Offline
                Pankaj Chamria
                wrote on last edited by
                #7

                The processing is actually on a background thread. And the only work that the UI thread is doing is updating itself whenever the INotifyPropertyChanged event is fired for a property. I think with WPF such high frequency of updates is bound to be detrimental to performance. I made a prototype in windows forms and that worked much better under the same load. One more intersting point I observed is that Virtualization is by default true in ListView. Hence making IsVirtualizing = true explicitly, did not have any impact. After trying out many options I am now gonna make use of the good old Thread.Sleep() to add re-curring delays in milliseconds while updating columns of the ListView. Its not an ideal way but i cant figure out anything else:-(

                Pankaj Chamria, Software Programmer.

                K 1 Reply Last reply
                0
                • P Pankaj Chamria

                  The processing is actually on a background thread. And the only work that the UI thread is doing is updating itself whenever the INotifyPropertyChanged event is fired for a property. I think with WPF such high frequency of updates is bound to be detrimental to performance. I made a prototype in windows forms and that worked much better under the same load. One more intersting point I observed is that Virtualization is by default true in ListView. Hence making IsVirtualizing = true explicitly, did not have any impact. After trying out many options I am now gonna make use of the good old Thread.Sleep() to add re-curring delays in milliseconds while updating columns of the ListView. Its not an ideal way but i cant figure out anything else:-(

                  Pankaj Chamria, Software Programmer.

                  K Offline
                  K Offline
                  Kunal Chowdhury IN
                  wrote on last edited by
                  #8

                  Hi, I am also facing the same issue while populating the huge collection of data in an ObservableCollection & binding it to the UI. Please help me in this case. If you have any code for this, please share that too. Regards, - K

                  P 1 Reply Last reply
                  0
                  • K Kunal Chowdhury IN

                    Hi, I am also facing the same issue while populating the huge collection of data in an ObservableCollection & binding it to the UI. Please help me in this case. If you have any code for this, please share that too. Regards, - K

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    If you're adding large amounts of data, then I would suggest that you consider my implementation of a custom observable collection here[^].

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    K 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      If you're adding large amounts of data, then I would suggest that you consider my implementation of a custom observable collection here[^].

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      K Offline
                      K Offline
                      Kunal Chowdhury IN
                      wrote on last edited by
                      #10

                      Yes... I have seen your link. But, my problem is like Pankaj. I am adding a huge range of data at the first time & then on every second I am modifying the list's item properties. There are more than 10 properties. FYI, I am not adding new items, just modifying the existing data. Due to huge property change in every second, there seems a performance issue as I am binding the data to the UI as UserControls. How can I achieve this? Please help me out. I am using WPF 3.5 SP1.

                      P 1 Reply Last reply
                      0
                      • K Kunal Chowdhury IN

                        Yes... I have seen your link. But, my problem is like Pankaj. I am adding a huge range of data at the first time & then on every second I am modifying the list's item properties. There are more than 10 properties. FYI, I am not adding new items, just modifying the existing data. Due to huge property change in every second, there seems a performance issue as I am binding the data to the UI as UserControls. How can I achieve this? Please help me out. I am using WPF 3.5 SP1.

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #11

                        Right, so you're bulk loading data and then modifying the individual items in the list, which results in multiple property changed events being raised. As only a finite amount of these items are being displayed, the first thing I'd do is wrap things up using a Virtualizing StackPanel.

                        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                        My blog | My articles | MoXAML PowerToys | Onyx

                        K 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Right, so you're bulk loading data and then modifying the individual items in the list, which results in multiple property changed events being raised. As only a finite amount of these items are being displayed, the first thing I'd do is wrap things up using a Virtualizing StackPanel.

                          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                          My blog | My articles | MoXAML PowerToys | Onyx

                          K Offline
                          K Offline
                          Kunal Chowdhury IN
                          wrote on last edited by
                          #12

                          I am already using the Virtualizing StackPanel, which loads more than 200 UserControls & displays 100 among them at a time> Each UserControl consists of 10 Properties. Is it require to use the Custom ObservableCollection? Let me know... how I can increase the performance... Share Code if you have one...

                          P 1 Reply Last reply
                          0
                          • K Kunal Chowdhury IN

                            I am already using the Virtualizing StackPanel, which loads more than 200 UserControls & displays 100 among them at a time> Each UserControl consists of 10 Properties. Is it require to use the Custom ObservableCollection? Let me know... how I can increase the performance... Share Code if you have one...

                            P Offline
                            P Offline
                            Pankaj Chamria
                            wrote on last edited by
                            #13

                            You seem to be having the same issue as i was with large number of property changed events getting raised.. Here are a few things you can try: 1) Out of all the columns make sure you raise changed events for only those properties that are changing in value. 2) In case you can afford to render the changes once every 1-2 seconds then following the approach given in the below link will be very useful. It solved my performance and UI hanging problems. Try this Link[^] Let me know if this helps.

                            Pankaj Chamria, Software Developer.

                            K 2 Replies Last reply
                            0
                            • P Pankaj Chamria

                              You seem to be having the same issue as i was with large number of property changed events getting raised.. Here are a few things you can try: 1) Out of all the columns make sure you raise changed events for only those properties that are changing in value. 2) In case you can afford to render the changes once every 1-2 seconds then following the approach given in the below link will be very useful. It solved my performance and UI hanging problems. Try this Link[^] Let me know if this helps.

                              Pankaj Chamria, Software Developer.

                              K Offline
                              K Offline
                              Kunal Chowdhury IN
                              wrote on last edited by
                              #14

                              Pankaj Chamria wrote:

                              1. In case you can afford to render the changes once every 1-2 seconds then following the approach given in the below link will be very useful. It solved my performance and UI hanging problems.

                              Can you elaborate the solution (if possible, paste the code) as this is very tough to understand a big post from the link you mentioned. :omg: :-D Please share the code to improve the performance for those huge data in the collection. In my case, it is getting hanged, when it is changing the property values. :( Please help. I am waiting for your reply.

                              P 1 Reply Last reply
                              0
                              • K Kunal Chowdhury IN

                                Pankaj Chamria wrote:

                                1. In case you can afford to render the changes once every 1-2 seconds then following the approach given in the below link will be very useful. It solved my performance and UI hanging problems.

                                Can you elaborate the solution (if possible, paste the code) as this is very tough to understand a big post from the link you mentioned. :omg: :-D Please share the code to improve the performance for those huge data in the collection. In my case, it is getting hanged, when it is changing the property values. :( Please help. I am waiting for your reply.

                                P Offline
                                P Offline
                                Pete OHanlon
                                wrote on last edited by
                                #15

                                You may want to take a look at what my friend Tamir came up with here[^].

                                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                My blog | My articles | MoXAML PowerToys | Onyx

                                1 Reply Last reply
                                0
                                • P Pankaj Chamria

                                  You seem to be having the same issue as i was with large number of property changed events getting raised.. Here are a few things you can try: 1) Out of all the columns make sure you raise changed events for only those properties that are changing in value. 2) In case you can afford to render the changes once every 1-2 seconds then following the approach given in the below link will be very useful. It solved my performance and UI hanging problems. Try this Link[^] Let me know if this helps.

                                  Pankaj Chamria, Software Developer.

                                  K Offline
                                  K Offline
                                  Kunal Chowdhury IN
                                  wrote on last edited by
                                  #16

                                  Pankaj Chamria wrote:

                                  1. Out of all the columns make sure you raise changed events for only those properties that are changing in value.

                                  I am changing all the properties.

                                  Pankaj Chamria wrote:

                                  1. In case you can afford to render the changes once every 1-2 seconds then following the approach given in the below link will be very useful. It solved my performance and UI hanging problems.

                                  Can you post your code for solving this problem?

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups