Search code examples
c#wpfmultithreadingtreeviewtreeviewitem

How can I access Tag property of TreeViewItem in a background thread?


I have a tree view in a wpf window. And for each tree view item in the tree view I store the related object in its Tag property.I am implementing search functionality on the tree view. And I run the search in a different thread so as to not block the UI. And during search operation I search the information in object stored in the tag property of tree view item. But the problem is that tag property is not accessible in a different thread. I do not want to use the tree view's dispatcher for accessing the tag property as then the program will switch between UI thread and background thread for every tag access. Then there will be no point in running the search in background. What can I do?


Solution

  • What can I do?

    You cannot access the Tag property of the TreeViewItem from any other thread than the one which it was originally created on, that is the UI/dispatcher thread.

    So you will have to either use the dispatcher to marshall the code that access the property back to the UI thread, run all code on the UI thread, or use a different approach that doesn't make use of the Tag property to store the information about the related object.

    These are your only options I am afraid.