如何绑定图像源?

时间:2011-04-23 00:28:34

标签: wpf image binding

从一些例子来看,我认为我这样做是正确的,但它没有用,所以我想在这里查看。我绑定了我的图像源,但图像不在那里。如果我带走了我的绑定并将Image源设置为下面方法中使用的路径,它就可以正常工作。

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" />

2 个答案:

答案 0 :(得分:7)

您应该将Source属性绑定到ImageSource,而不是Image。将biSource公开为属性并绑定到它而不是myImage

答案 1 :(得分:5)

您尝试将Image绑定到Image的来源,但无法正常工作。您需要将BitmapImage绑定到Image的源。

此外,BitmapImage需要作为公共财产公开。如果您不熟悉数据绑定,请阅读this overview on MSDN

此外,您应该学习how to debug bindings