Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind Image source?

Tags:

image

binding

wpf

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" />
like image 297
Terco Avatar asked Jun 28 '26 08:06

Terco


1 Answers

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

like image 160
Thomas Levesque Avatar answered Jun 30 '26 23:06

Thomas Levesque