Mahapps.metro: Treeview Default Style and Binding IsExpanded

Created on 13 Nov 2017  路  4Comments  路  Source: MahApps/MahApps.Metro

Hello,

I'm Using Mahapps for a while.
I'm starting a new project with only a treeview.
I'm using a Hierarchical Datatemplace.
No trouble to bind my observable collection and display it in the treeview.

The trouble is : My model have a property Expanded (bool) and i can't bind it in hierarchicalDatatemplate (logical it's for display data, not for setup / interact with the treeview) .

_Example :_

<TreeView.ItemTemplate>

                    <HierarchicalDataTemplate  ItemsSource="{Binding Subnodes}">                        

                        <StackPanel Orientation="Horizontal" ToolTip="{Binding Path}">
                            <iconPacks:PackIconModern  ToolTip="{Binding Path}"  Kind="Folder" Width="12" Height="12"></iconPacks:PackIconModern>
                            <TextBlock TextDecorations="Underline" ToolTip="{Binding Path}" Text="{Binding DisplayName}" Padding="2,0,0,0" FontSize="10" 
                                       Foreground="{Binding Path=Couleur,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}">
                                <TextBlock.InputBindings>
                                    <MouseBinding MouseAction="LeftDoubleClick"
                        Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type Window}}, Path=DataContext.Exec}"
                        CommandParameter="{Binding}"/>
                                </TextBlock.InputBindings>
                                <TextBlock.Effect>
                                    <DropShadowEffect RenderOptions.ClearTypeHint="Enabled"
                                          ShadowDepth="0"
                                          Direction="330"
                                          Color="Black"
                                          Opacity="0.75"
                                         BlurRadius="2"/>
                                </TextBlock.Effect>
                            </TextBlock>
                        </StackPanel>                        
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>

The only way i found on the web is using Style like this :

                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsExpanded" Value="{Binding Expanded}"/>   
                    </Style>
                </TreeView.ItemContainerStyle>

And yes it's work but i loose the Mahapps style and essentially the "fullrowselect" effect.

Do someone knows how i could bind this property to my object avoid loosing the fullrowselect effect ?

Thx in advance for your help and sorry very sorry for my poor english!

image

Most helpful comment

You're my god.. you save my life.
I'm not good enough in Xaml... i didn't know the BaseOn..
I try and it works. Thx you very much for your Help. and thx for all your works.

All 4 comments

@Zebody Hi, it looks like you forgot to inherit the base MahApps style for the TreeViewItem

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
        <Setter Property="IsExpanded" Value="{Binding Expanded}"/>
    </Style>
</TreeView.ItemContainerStyle>

You're my god.. you save my life.
I'm not good enough in Xaml... i didn't know the BaseOn..
I try and it works. Thx you very much for your Help. and thx for all your works.

Thanks for the help on posting this fix. Took a day to get to this thread :)

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webprofusion-chrisc picture webprofusion-chrisc  路  10Comments

SamOxyPlot picture SamOxyPlot  路  14Comments

kshkrao3 picture kshkrao3  路  12Comments

AndrzejKl picture AndrzejKl  路  12Comments

ghost picture ghost  路  18Comments