Terco
Terco

Reputation: 920

How to bind Image source?

From some examples, I think I'm doing this right, but it's not working so I wanted to check here to see. I'm binding my Image source, but the image is not there. If I take away my binding and just set the Image source to the path used in the method below it works fine.

public Image myImage = new Image();

public void someMethod() {
    BitmapImage biSource = new BitmapImage();
    biSource.BeginInit();
    biSource.CacheOption = BitmapCacheOption.OnLoad;
    biSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    biSource.UriSource = new Uri(@"C:\Images\testimage.jpg");
    biSource.DecodePixelWidth = 150;
    biSource.EndInit();

    myImage.Source = biSource;
}

xaml code
<Image Source="{Binding Path=myImage}" Width="150" Height="150" />

Upvotes: 3

Views: 4499

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292645

You should bind the Source property to an ImageSource, not an Image. Expose biSource as a property and bind to it instead of myImage

Upvotes: 7

brunnerh
brunnerh

Reputation: 185290

You try to bind an Image to the source of an Image, not going to work. You need to bind the BitmapImage to the source of the Image.

Also the BitmapImage needs to be exposed as public property. If you are new to data binding read this overview on MSDN.

Further you should learn how to debug bindings.

Upvotes: 5

Related Questions