【问题标题】:How to bind Image source?如何绑定图片源?
【发布时间】:2011-08-11 07:15:31
【问题描述】:

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

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

【问题讨论】:

    标签: wpf image binding


    【解决方案1】:

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

    【讨论】:

    • Ts,我快了半分钟 :(
    • @H.B.,抱歉,没有注意到您的回答...这是 +1 来弥补它;)
    • 哈哈,谢谢,反正也不是你的错,很多时候同时回答。
    【解决方案2】:

    您尝试将Image 绑定到Image 的源,但不起作用。您需要将BitmapImage 绑定到Image 的源。

    BitmapImage 也需要作为公共属性公开。如果您不熟悉数据绑定,请阅读 this overview on MSDN

    你应该进一步了解how to debug bindings

    【讨论】:

    • 我通读了概述并研究了调试绑定。感谢您的指导。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多