我正在尝试为我的图像源绑定应用转换器。这是我的xml:
<Window.Resources>
<DataTemplate x:Key="listBoxTemplate">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<ImageConverter x:Key="MyImageConverter" />
</StackPanel.Resources>
<Image Source="{Binding Path=thumb, StringFormat=/WpfTest;component/Images/{0}, Converter={StaticResource MyImageConverter}}" Height="100" Width="130" Margin="5"></Image>
<StackPanel Orientation="Vertical" Width="247">
<TextBlock Text="{Binding recipeName}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="{Binding cuisine}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</Window.Resources>
这是我的 imageConverter 类:
using System;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media.Imaging;
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string path = (string)value;
try
{
//ABSOLUTE
if (path.Length > 0 && path[0] == System.IO.Path.DirectorySeparatorChar
|| path.Length > 1 && path[1] == System.IO.Path.VolumeSeparatorChar)
return new BitmapImage(new Uri(path));
//RELATIVE
return new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + path));
}
catch (Exception)
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
但是当我尝试运行应用程序时,它返回“找不到类型 imageconverter”并且 VS 突出显示该部分
<ImageConverter x:Key="MyImageConverter" />
在上面的 xaml 中。我如何解决它?(顺便说一句,我从Wpf 获得了 imageconverter 代码 - 相对图像源路径)