如何将TreeView图标更改为文件夹图标?

9

我想把我的TreeView的图标改成文件夹图标。当它折叠时,也需要有一个打开的文件夹图标。

我的TreeView中有数据绑定项,代码如下:

<TreeView x:Name="TreeViewCategories"  Grid.Row="0" Grid.Column="1" Height="610" HorizontalAlignment="Left"  Margin="29,111,0,0" VerticalAlignment="Top" Width="315" BorderThickness="0" Background="Transparent" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate  ItemsSource="{Binding Items}">
            <TextBlock FontSize="20" Text="{Binding Name}" PreviewMouseDown="TextBlock_PreviewMouseDown"/>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这是我如何从XML中添加项到树形视图的代码(这只是一小部分代码):

private void LoadHospitalXML()
{
    try
    {
        FileStream fs = new FileStream("ConfigOrgHospital.xml", FileMode.Open, FileAccess.Read);

        var xml = XmlReader.Create(fs);

        rootElement = ConvertHospitalData(xml);

        this.TreeViewCategories.ItemsSource = null;
        List<HospitalWrapper> li = new List<HospitalWrapper>();
        var hosp = rootElement.Items.FirstOrDefault();
        if (hosp != null)
        {
            foreach (var i in hosp.Hospital)
            {
                li.AddIfNotNull(CreateHospList(i));
            }
        }

        this.TreeViewCategories.ItemsSource = li;
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

}

private HospitalWrapper CreateHospList(object obj)
{
    var newItem = new HospitalWrapper();

    newItem.Context = obj;
    //Hospital Names//
    if (obj is HospitalDataHospitalsHospital)
    {
        var hosp = (HospitalDataHospitalsHospital)obj;

        //newItem.Title = "Hospitals";
        newItem.Name = hosp.DefaultName;

        var tmp = new HospitalWrapper();
        tmp.Name = "Sites";
        tmp.IsTitle = true;

        if (hosp.Sites != null)
            foreach (var i in hosp.Sites)
            {
                tmp.Items.AddIfNotNull(CreateHospList(i));
            }
        newItem.Items.Add(tmp);

        tmp = new HospitalWrapper();
        tmp.Name = "Specialties";
        tmp.IsTitle = true;

        if (hosp.Deps != null)
            foreach (var j in hosp.Deps)
            {

                tmp.Items.AddIfNotNull(CreateHospList(j));
            }

        newItem.Items.Add(tmp);
    }
}
1个回答

13

顺便说一下,我几天前做了类似的事情。在我的应用程序中,文件夹图标被添加到HierarchicalDataTemplate中,用于那些像文件夹一样的对象,我使用触发器来根据项是否展开来更改图标,这里是相关的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>

其中 {StaticResource Icon_FolderOpen}{StaticResource Icon_FolderClosed} 是包含文件夹状态图标的 BitmapImagesIconImageStyleSmall 是一种样式,它设置了图像的 MaxWidthMaxHeight 为适当的值。


编辑:为了完整起见。

<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>

使用的图标


我也在做这个,但是我一直遇到的一个问题是当文件夹中的所有子项都被移除时,我希望它显示FolderClosed图标。有没有简单的方法可以做到这一点,而不需要观察子项的CollectionChanged事件?(树形视图仅在存在子项时显示展开箭头,因此如果可能的话,我想要连接到相同的机制。) - NielW
1
@NielW:我不知道,你可能需要提出一个单独的问题,也许有其他人会知道。就我个人而言,已经有一段时间没有使用过WPF了。 - H.B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接