我已经为ListView定义了一个DataTemplate来显示fileInfo详细信息。这是DataTemplate
<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> ListView的ImagesSource为List<FileInfo>
我必须根据文件的扩展名在列表中添加一个自定义图标。是否可以将扩展传递给一个方法,以获取现有DataTemplate中的图标路径?
发布于 2010-09-17 20:23:42
你需要一个转换器:
[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}}" />https://stackoverflow.com/questions/3734724
复制相似问题