如何绑定图像源?

Ter*_*rco 3 wpf binding image

从一些例子来看,我认为我这样做是正确的,但它没有用,所以我想在这里查看.我绑定了我的图像源,但图像不在那里.如果我拿走我的绑定并将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" />
Run Code Online (Sandbox Code Playgroud)

Tho*_*que 7

你应该将Source属性绑定到一个ImageSource,而不是一个Image.biSource作为属性公开并绑定到它而不是myImage


H.B*_*.B. 5

你试图绑定一个ImageImage,而不是工作.你需要绑定BitmapImage到的源Image.

BitmapImage需要作为公共财产公开.如果您不熟悉数据绑定,请阅读MSDN上的此概述.

此外,您应该学习如何调试绑定.