绑定图像源,以TextBox.Text
问题描述:
我尝试使用下面的代码名为“txtImage”到图像的文本框的文本约束没有结果:绑定图像源,以TextBox.Text
<Image Source="{Binding ElementName=txtImage, Path=Text}" />
什么会正确的方法是什么?
答
图像的来源需要的BitmapImage,因此尝试使用值转换器将字符串转换为图像:
public sealed class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
try
{
return new BitmapImage(new Uri((string)value));
}
catch
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding ElementName=txtImage, Path=Text, Converter=...}" />
</Image.Source>
</Image>
+0
为了达到这个目的,转换器必须在程序代码中被删除吗? – Johnathan1 2009-07-21 00:57:22
您是否获得绑定从Visual Studio运行时输出中的错误?我怀疑你可以使用String作为ImageSource。 – Joey 2009-07-16 15:11:02