WPF TreeView style template resetting IsExpanded on style switch

253 views Asked by At

I have a basic TreeView on a window that has a style applied from a resource in a dll. The style dll is capable of switching between two styles. When I don't have a style for the TreeView in the dll I am able to expand the TreeViewItems, switch styles (of other controls), and the TreeViewItems remain expanded. However, as soon as I add a style for the TreeView, the TreeViewItems that have been expanded collapse as soon as the style is switched.

All fancy animations and TreeViewItem styling has been removed in order to track down the problem. The remaining style is simply:

<Style x:Key="{x:Type TreeView}" TargetType="{x:Type TreeView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeView">
                <Border Name="Border"
                        Background="Transparent"
                        BorderThickness="1"
                        CornerRadius="1">

                    <ItemsPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Any suggestions would be greatly appreciated. I've ran out of ideas!

1

There are 1 answers

3
dev hedgehog On

When you take away a template or replace by another some dependency properties will reset to their default value. The reason for that is dependency property precedence. Take a look at this here: http://msdn.microsoft.com/library/ms743230

Think of it like this: A dependency property may contain many values which are layered. The value in top most layer is always the current value. If you take a style away, you take layers away too. If you take all layers away, the dependency property will take default value as current value.

When you replace the style of your TreeView, all underneath styles will be updated/reinitalized/changed/resetted...

To fix this try keep same template and only change colors, borders and stuff like that.. or use Binding