How can I change the TreeView Icon into a folder icon?

前端 未结 1 1704
一生所求
一生所求 2021-02-15 14:16

I\'m trying to change the icon of my TreeView in a folder icon. Also when it collapses it needs to have an opened folder icon.

My treeview has databound items in it and

相关标签:
1条回答
  • 2021-02-15 14:53

    Incidentally i did something like this just a few days ago. In my application a folder icon is added in the HierarchicalDataTemplate to those objects which behave like folders, i use a trigger to change the icon based on whether the item is expanded or not, here's the relevant bit of XAML:

        <HierarchicalDataTemplate DataType="{x:Type data:FeedComposite}"
                                  ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal" Margin="1">
                <StackPanel.Children>
                    <Image>
                        <Image.Style>
                            <Style BasedOn="{StaticResource IconImageStyleSmall}" TargetType="Image">
                                <Setter Property="Source" Value="{Binding Source={StaticResource Icon_FolderClosed}, Mode=OneTime}"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
                                        <Setter Property="Source" Value="{Binding Source={StaticResource Icon_FolderOpen}, Mode=OneTime}"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <TextBlock Text="{Binding Title}"/>
                </StackPanel.Children>
            </StackPanel>
        </HierarchicalDataTemplate>
    

    Where {StaticResource Icon_FolderOpen} and {StaticResource Icon_FolderClosed} are BitmapImages that hold the icons for the folder states. IconImageStyleSmall is a style which sets the MaxWidth and MaxHeight of the images to something appropriate.


    Edit: For completion's sake.

    <BitmapImage x:Key="Icon_FolderOpen"   UriSource="pack://application:,,,/ImageResources/Icons/FolderOpen.ico"   />
    <BitmapImage x:Key="Icon_FolderClosed" UriSource="pack://application:,,,/ImageResources/Icons/FolderClosed.ico" />
    
    <Style x:Key="IconImageStyleSmall" TargetType="Image">
        <Setter Property="MaxWidth" Value="16"/>
        <Setter Property="MaxHeight" Value="16"/>
        <Setter Property="Margin" Value="1"/>
    </Style>
    

    Icons used

    0 讨论(0)
提交回复
热议问题