Wpf数据模板

Wpf数据模板,wpf,datatemplate,Wpf,Datatemplate,我已经为ListView定义了一个DataTemplate来显示fileInfo详细信息。 这是数据模板 <DataTemplate x:Key="srchFileListTemplate"> <StackPanel> <WrapPanel> <TextBlock FontWeight="Bold" FontFamily="Century Gothic" Text="File

我已经为ListView定义了一个DataTemplate来显示fileInfo详细信息。 这是数据模板

<DataTemplate x:Key="srchFileListTemplate">
    <StackPanel>
        <WrapPanel>
            <TextBlock FontWeight="Bold" FontFamily="Century Gothic"
                Text="FileName :"/>
            <TextBlock Margin="10,0,0,0" FontWeight="Bold"
                FontFamily="Century Gothic" Text="{Binding Path=Name}"/>
        </WrapPanel>
        <WrapPanel>
            <TextBlock FontFamily="Century Gothic" Text="FilePath :"/>
            <TextBlock Margin="20,0,0,0" FontFamily="Century Gothic"
                Text="{Binding Path = DirectoryName}"/>
        </WrapPanel>
        <WrapPanel>
            <TextBlock FontFamily="Century Gothic" Text="File Size :"/>
            <TextBlock Margin="20,0,0,0" FontFamily="Century Gothic"
                Text="{Binding Path = Length}"/>
            <TextBlock Text="Bytes"/>
        </WrapPanel>
        <WrapPanel>
            <TextBlock FontFamily="Century Gothic" Text="File Extension:"/>
            <TextBlock Margin="20,0,0,0" FontFamily="Century Gothic"
                Text="{Binding Path = Extension}"/>
        </WrapPanel>
    </StackPanel>
</DataTemplate> 

List视图的
ImagesSource
List

我必须根据列表中文件的扩展名添加自定义图标。是否可以将扩展传递给方法以获取现有DataTemplate中的图标路径?

您需要一个转换器:

[ValueConversion(typeof(string), typeof(ImageSource))]
public class FileIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string fileName = value as string;
        if (fileName == null)
            return null;
        return IconFromFile(fileName);
    }

    private ImageSource IconFromFile(string fileName)
    {
        // logic to get the icon based on the filename
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // The opposite conversion doesn't make sense...
        throw new NotImplementedException();
    }

}
然后需要在参考资料中声明转换器的实例:

<Window.Resources>
    <local:FileIconConverter x:Key="iconConverter" />
</Window.Resources>

您可以在绑定中使用它,如下所示:

<Image Source="{Binding FullName, Converter={StaticResource iconConverter}}" />


我遇到的问题是图像源不是静态的,我必须根据扩展名获取图像源,并且必须将图像包含在ListView的数据模板中我不理解您的评论。。。“在数据模板中包含图像”是什么意思?