问题描述
我正在尝试为我的图像源绑定应用转换器.这是我的 xaml:
i'm trying to apply a converter for my image source binding. here's my xaml:
<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 类:
and here's my imageConverter class:
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 突出显示该部分
but when i tried running the apps, it returns "the type imageconverter was not found" and VS highlight the part
<ImageConverter x:Key="MyImageConverter" />
在上面的 xaml 中.我如何解决它?(顺便说一句,我从 Wpf - 相对图像源路径 获得了 imageconverter 代码)
in the xaml above. How do I fix it? (Btw I got the imageconverter code from Wpf - relative image source path )
推荐答案
你需要像这样添加它的命名空间:
You need to add its namespace like this:
<ns:ImageConverter x:Key="MyImageConverter"/>
并确保您像这样将命名空间添加到更高的位置:
And make sure you added the namespace higher up like this:
<DataTemplate xmlns:ns="....">
实际的命名空间取决于您的项目,但代码完成会对您有所帮助.
The actual namespace depends on you project but code completion will help you.
这篇关于未找到类型 imageconverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!