locked
XAML: setting BitmapImage.UriSource from binding doesn't work RRS feed

  • Question

  • The following should display a picture for a list box item:

    <Window.Resources>
        <DataTemplate x:Key="ImageTemplate">
          <Image>
            <Image.Source>
              <BitmapImage UriSource="{Binding}" />
            </Image.Source>
          </Image>
        </DataTemplate>
      </Window.Resources>
     
      <Grid>
        <ListBox>
          <ListBoxItem ContentTemplate="{StaticResource ImageTemplate}">
            C:\picture.jpg
          </ListBoxItem>
        </ListBox>
      </Grid>

    ... but I get an exception: "Property 'UriSource' or property 'StreamSource' must be set."

    Simply using <Image Source="{Binding}"/> would work in this example, but I want to use the BitmapImage for some of its other properties. <BitmapImage UriSource="\path\to\image.jpg" /> works as well.

    Does anyone have an idea how to do this?

        -Kolja


    Wednesday, October 11, 2006 8:51 AM

Answers

  • you need to have a converter which can take a string and return an image, convert method will contain something like this 

    BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(value as string);
                image.EndInit();

                return image;

    Wednesday, October 11, 2006 9:37 AM

All replies

  • you need to have a converter which can take a string and return an image, convert method will contain something like this 

    BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(value as string);
                image.EndInit();

                return image;

    Wednesday, October 11, 2006 9:37 AM
  • Thanks, that works! A converter is probably the nicest way to express that part in procedural code...

    BTW, I also tried to do something like <BitmapImage UriSource="{Binding Path=MyImagePath, Converter={StaticResource uriConverter}" /> to check if I could just convert the string to a URI myself, but as it turns out, that converter is never even called. Seems like data binding just doesn't work at all on the BitmapImage...


    -Kolja
    Wednesday, October 11, 2006 1:05 PM
  • I have the same problem:

    Binding on BitmapSource.UriSource does not work - the bound property is never called and the above mentioned exception is thrown.

    Monday, November 13, 2006 2:23 PM
  • I have an interesting work-around you might try:

    http://www.wiredprairie.us/journal/2007/03/bitmapimageurisource_binding_d.html

    It's a trick in a way, but it can help in this situation.

    Sunday, March 25, 2007 8:53 PM
  • I've been having the same problem too.  Been having a heck of a time trying to link XAML and code behind.  I'm new to WPF and XAML (intermediate on VB05) and can't tell if it's my illiteracy or the program or what.  And the help documentation all seems to come just shy of telling you how to link image in code behind to XAML, as if it's so simple that everyone has just passed it by to deal with more advanced topics.  Just finding the right place to vent has been a multi weekend challenge!  (sorry to go on so--just quite frustrated)

    Monday, December 31, 2007 4:37 PM