I have Defined a DataTemplate for ListView to Display the fileInfo details. This is the 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>
ImagesSource
for th开发者_开发技巧e ListView
is List<FileInfo>
I have to add a customized icon according to the Extension of the file to the List. Is it possible to pass the extension to a method to get the icon path in the existing DataTemplate?
You need a converter:
[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();
}
}
You then need to declare an instance of the converter in the resources:
<Window.Resources>
<local:FileIconConverter x:Key="iconConverter" />
</Window.Resources>
And you use it in your binding as follows:
<Image Source="{Binding FullName, Converter={StaticResource iconConverter}}" />
精彩评论