1

I am trying to bind the Text of a TextBox named 'txtImage' to an image using the following code with no results:

<Image Source="{Binding ElementName=txtImage, Path=Text}" />

What would the right approach be?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Johnathan1
  • 2,241
  • 5
  • 23
  • 24
  • Are you getting binding errors in the output when running from Visual Studio? I'd doubt that you can use a String as an ImageSource. – Joey Jul 16 '09 at 15:11

1 Answers1

2

Source of an Image requires a BitmapImage, thus try using a Value Converter to convert the string to an Image:

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>

Reference: Image UriSource and Data Binding

Community
  • 1
  • 1
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360