MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.28k stars 2.45k forks source link

Treeview Default Style and Binding IsExpanded #3103

Closed Zebody closed 6 years ago

Zebody commented 6 years ago

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

punker76 commented 6 years ago

@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>
Zebody commented 6 years ago

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.

pwrp4j1 commented 6 years ago

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

MarianoPassarini commented 4 years ago

Thanks