如何将TreeView图标更改为文件夹图标? [英] How can I change the TreeView Icon into a folder icon?

查看:727
本文介绍了如何将TreeView图标更改为文件夹图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文件夹图标中更改TreeView的图标。当它崩溃时,它需要有一个打开的文件夹图标。

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 the code is:

<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中的项目填充树视图的方式(它是一个从很多代码中删除的东西) :

Also this is how I fill the treeview with items from XML (It's a snipped out of alot of code:

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);
    }
}


推荐答案

顺便提一下几天前我做了类似的事情。在我的应用程序中,HierarchicalDataTemplate中的文件夹图标添加到那些行为类似于文件夹的对象,我使用触发器根据项目是否展开来更改图标,这里是XAML的相关位:

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>

其中 {StaticResource Icon_FolderOpen} {StaticResource Icon_FolderClosed} BitmapImages ,其中包含文件夹状态的图标。 IconImageStyleSmall 是一种设置 MaxWidth MaxHeight 的样式适当的图像。

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.

编辑:为了完成起见。

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

使用的图标

这篇关于如何将TreeView图标更改为文件夹图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆