开发者

WPF Update binding properties of multiple items contained in a list at the same time

开发者 https://www.devze.com 2023-01-27 16:09 出处:网络
I\'m binding a list of object to an ItemsControl which contain a TextBox control. I want to be able to change the textbox background color based on the background color property of the bind object.

I'm binding a list of object to an ItemsControl which contain a TextBox control.

I want to be able to change the textbox background color based on the background color property of the bind object.

It works perfectly with the INotifyPropertyChanged interface but when I need to update let say 1000 objects it takes a huge amount of time I guess because it needs to update the controls one by one.

Does someone has a tip for me to lets say, update all my objects background color and than update the binding in one shot instead of object by object?

Thanks,

开发者_JAVA技巧

Mat


If you use virtualization, it'll only need to update the items that are visible - normally items controls will only track property changes on the items that are currently visible. However, if you're using the base ItemsControl, virtualization will be off by default. To turn it on, you need to provide a custom template that includes a ScrollViewer with CanContentScroll set to True, and you also need to use a VirtualizingStackPanel, either in the control template, or via the ItemsPanel - this shows the former approach:

<ItemsControl ItemsSource="...whatever...">
  <ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
      <ScrollViewer CanContentScroll="True">
        <VirtualizingStackPanel IsItemsHost="True" />
      </ScrollViewer>
    </ControlTemplate>
  </ItemsControl.Template>
</ItemsControl>

Virtualization improves performance with large numbers of list items in various ways. It should help your scenario, but it may also improve other aspects of the performance. It's not without its problems, but I'd definitely try it here first before attempting other solutions.


I finally found why it was taking me so long to update my UI. Byt the way, What I'm trying to achieve is a kind of spreadsheet grid. My cell container was a border with the borders visible. I was updating the Border control Background color based on cell selection. Disabling the borders of the Border control resolved my issue right away. The UI update time went from 4-5 seconds to instantaneous.

Thank you guys for your answer, I will still try to implement your suggestion to improve even more my project.

Thanks

Mat

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号