值转换器未在树视图中加载图像

时间:2017-12-21 08:26:14

标签: c# wpf xaml

我是C#的初学者。尝试遵循本教程:https://www.youtube.com/watch?v=6OwyNiLPDNw

我目前创建了一个TreeView来显示系统中的所有文件夹/文件,如上面的视频所述。我添加了一个valueConverter类来根据项目是否为驱动器/文件夹/文件来更改图标。相应地有三个文件。现在,我面临的问题是,当我运行程序时,没有抛出任何异常,但图像不会显示。如果我在相应的xaml文件中添加静态图像文件,我会看到它已显示出来。但是如果我使用了valueConverter,那就不是了。我单步进入程序,我可以看到我的valueConverter函数被成功调用,并且也选择了相应的图像。我不明白为什么没有显示图像。

非常感谢任何帮助!

我的xaml代码:

<Window x:Class="Wpf_TreeView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Wpf_TreeView"
    Loaded="Window_Loaded"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <TreeView x:Name="FolderView">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="100" Margin="3" 
                                       Source="{Binding 
                                       RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TreeViewItem}},
                                       Path=Tag,
                                       Converter={x:Static local:HeaderToImageConverter.Instance}}"/>
                                <TextBlock VerticalAlignment="Center" Text="{Binding}"/>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


        </TreeView.Resources>

我的价值转换器代码:

namespace Wpf_TreeView
{
    /// <summary>
    /// Convert a full path to a specific image type of a drive,folder or a file
    /// </summary>

    [ValueConversion(typeof(string), typeof(BitMapImage))]
    public class HeaderToImageConverter : IValueConverter
    {
        public static HeaderToImageConverter Instance = new HeaderToImageConverter();

        //public static HeaderToImageConverter Instance { get => instance; set => instance = value; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Get the full path
            var path = (string)value;

            //Check if path is null
            if (path == null) return null;

            //Get the name of the file/folder
            var name = MainWindow.GetFileFolderName(path);

            // By default we presume file image
            var image = "Images/file.png";

            // If the name is blank, we assume it's a drive (file/folder) cannot have blank name
            if (string.IsNullOrEmpty(name))
            {
                image = "Images/drive.png";
            }
            else if (new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory))
            {
                image = "Images/folder.jpg";
            }
            //the below statement does not seem to work although image contains the proper string as expected
            return new BitmapImage(new Uri($"pack://application:,,,/{image}")); 
        }

    }
}

1 个答案:

答案 0 :(得分:-2)

解决方案是使用BitmapImage代替BitMapImage。应该警惕使用alt + enter而不注意它的作用!