0

I am using the TreeView control from the WinrtXamlToolkit in a uwp app. I want to apply a different style to some TreeViewItems depending on a conditon so I created a class, TreeViewItemStyleSelector which derives from StyleSelector and I override the SelectStyleCore function.

 public Style ResourceStyle { get; set; }

 public Style ClassroomStyle { get; set; }

 protected override Style SelectStyleCore(object item, DependencyObject container)
        {
            // control never reaches here. 
            // logic to apply style
        }

Then in xaml I use it like this.

In Page Resources

<StyleSelectors:TreeViewItemStyleSelector ResourceStyle="{StaticResource AStyle}" ClassroomStyle = "{StaticResource BStyle}"/> 

And later in the page.

<wxtControls:TreeView ItemsSource="{Binding StructureViewModels}" ItemContainterStyleSelector="{StaticResource TreeViewItemStyleSelector}" />

The problem is that the SelectStyleCore override is never called. Does anybody know why?

Corcus
  • 1,070
  • 7
  • 25

1 Answers1

0

I am not yet sure what's the reason this doesn't work, although I have some theories. One is - this was never implemented. Perhaps at least at the root level it should work because it's an ItemsControl, but because of the way it's implemented (hierarchically) - the ItemContainerStyleSelector would have to be forwarded from the TreeView to the TreeViewItems, which it isn't.

I haven't had a chance to try to reproduce it yet, but if I were to try to fix it or work around it - I would first try forwarding that property in HeaderedItemsControl.cs - roughly where it says "// Note: this is where we would apply the HeaderTemplateSelector (...) (if implemented)". The alternative (if you don't want to modify the toolkit's code) might be to specify the template for the TreeViewItem and in the template - use a StyleSelector on template parts you want to be different for different data items.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Thanks for your answer. I already went for option 2 along with tampering with the style of the TreeViewItem. Thanks again for taking time to look at this and for an awesome toolkit :) – Corcus Jul 24 '16 at 10:11