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 P开发者_开发知识库ath=myImage}" Width="150" Height="150" />
You should bind the Source
property to an ImageSource
, not an Image
. Expose biSource
as a property and bind to it instead of myImage
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.
精彩评论