1

I use a DataTemplateSelector for a ListView column headers template selection. ListView itself is defined in a DataTemplate and has a few tabs. So, in practice, I have the same DataTemplate (so ListView too) applied to several TabItems. This means that if I select tab {A} and set XDataTemplate on the ListView column {AColumn}, then switch the tab, lets say to tab {B}, on {B}'s ListView (that is always the same one) column {AColumn}, we will see the same XDataTemplate as they share same UI data. So I created data layer where I hold relational information about {Tab} <-> {ListView:Column} <-> {HeaderContent}. This actually reads DatatemplateSelector in order to correctly update the UI on user screen. How can I notify the DataTemplateSelector to update the current view as needed on request?

Thank you.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Tigran
  • 61,654
  • 8
  • 86
  • 123

1 Answers1

3

I found a solution to this problem and it wasn't quite as difficult as I thought. The template is applied to the ListView column's header. This is enough to vary the header's value in order to generate WPF's internal event that will execute SelectTemplate on DataTemplateSelector automatically.

Here is sample code:

if (MyListView != null)
 {
      foreach (GridViewColumn col in (MyListView.View as GridView).Columns)
      {
            string header = col.Header.ToString();
            col.ClearValue(GridViewColumn.HeaderProperty);
            col.SetValue(GridViewColumn.HeaderProperty, header);
      }
 }

That's it. Hope this will help someone in the future.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Tigran
  • 61,654
  • 8
  • 86
  • 123