0

I have a treeview, for which I wrote the style template of treeview in App.xaml, as I need to apply the style in two or three treeviews in my application. Now my problem is that the togglebutton style works for parent and not for child nodes. Here for the togglebutton collapse and expand, i added two images( Resources/Images/arrowexpand.png and Resources/Images/arrowcollapse.png It is working perfectly with parent nodes, but not with child and subchild nodes. For the child and subchild nodes, the default triangle button comes. I didn't use MVVM. I don't know where I am going wrong

In the treeview

<TreeView x:Name="myTreeview" ItemContainerStyle="{StaticResource ms}"/>

Here is my code in App.xaml

<Style TargetType="TreeView">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeView">
                <Border Name="Border" CornerRadius="5" BorderThickness="2">
                    <Border.BorderBrush>
                        <SolidColorBrush Color="DarkGreen"/>
                    </Border.BorderBrush>
                    <ItemsPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Image x:Name="image" Source="Resources/Images/arrowcollapse.png" />
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter TargetName="image" Property="Source" Value="Resources/Images/arrowexpand.png" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style  x:Key="ms" TargetType="TreeViewItem" >
    <Setter Property="IsExpanded" Value="True"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Padding" Value="1,0,0,0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="19" Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                <!--     Connecting Lines -->
                   <Rectangle x:Name="HorLn" Margin="9,1,0,0" Height="1" Stroke="#DCDCDC" SnapsToDevicePixels="True"/>
                    <Rectangle x:Name="VerLn" Width="1" Stroke="#DCDCDC" Margin="0,0,1,0" Grid.RowSpan="2" SnapsToDevicePixels="true" Fill="White"/>
                    <ToggleButton Margin="-1,0,0,0" x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                    <ToggleButton Margin="-1,0,0,0" x:Name="Collapsed" Style="{StaticResource ExpandCollapseToggleStyle}" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                    <Border Name="Bd" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MinWidth="20"/>
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                </Grid>


               <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="False">
                        <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                    </Trigger>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                    </Trigger>

                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader" Value="false"/>
                            <Condition Property="Width" Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader" Value="false"/>
                            <Condition Property="Height" Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                    </MultiTrigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd" Property="Background" Value="Green"/>
                        <Setter Property="Foreground" Value="White"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I want the arrow button to be for the child items and subchild items. Please tell me where I am wrong

grek40
  • 13,113
  • 1
  • 24
  • 50
Lavanya
  • 37
  • 1
  • 7
  • Possible duplicate of [Apply style to all TreeViewItem](https://stackoverflow.com/questions/1366598/apply-style-to-all-treeviewitem) – grek40 Mar 27 '19 at 21:55
  • I tried this. It's not working for me. Any other way... – Lavanya Mar 28 '19 at 08:47
  • What exactly did you try? – grek40 Mar 28 '19 at 10:08
  • All my style statements are inside App.xaml – Lavanya Mar 28 '19 at 10:35
  • This should work, unless you have some interfering style definitions in your project or some outdated generated files. Make a solution cleanup (rebuild all is not enough) then retry the resources approach. Make sure that you project doesn't contain any style with target TreeViewItem that could interfere with your wanted style – grek40 Mar 28 '19 at 12:53
  • It's working now. I just caught your point. I am deriving a class from my treeviewitem. so the style is not applied for the derived class. Then I added this line in the constructorStyle = (Style)FindResource(typeof(TreeViewItem)); Then it started working. – Lavanya Mar 28 '19 at 13:21
  • With the updated information, there is a better duplicate: [Default styles do not apply to subclasses](https://stackoverflow.com/q/16677479/5265292). Maybe you can try the presented solution from there instead of the constructor style assignment. – grek40 Mar 29 '19 at 07:08

1 Answers1

0

I found out the Solution. Actually I am trying to apply the styles for the derived classes of the treeview item. So only my style was not working. What I did was I just added this style statement in my Constructor of the derived Class Style = (Style)FindResource(typeof(TreeViewItem)); Then the style was applied to all the items in the treeview.

Lavanya
  • 37
  • 1
  • 7