initial image in WPF Image Control

南笙酒味 提交于 2019-12-01 08:35:40

You might be able to make it work using TargetNullValue on the binding, only set the image property when it is loaded.

e.g.

<BitmapImage x:Key="DefaultImage" UriSource="Images/Error.ico" />
<Image Source="{Binding TestBitmapImage,
                        TargetNullValue={StaticResource DefaultImage}}" />
private BitmapImage _TestBitmapImage = null;
public BitmapImage TestBitmapImage
{
    get { return _TestBitmapImage; }
    set
    {
        if (_TestBitmapImage != value)
        {
            _TestBitmapImage = value;
            PropertyChanged.Notify(() => this.TestBitmapImage);
        }
    }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    var img = new BitmapImage();
    img.DownloadCompleted += (s, dcea) =>
        {
            TestBitmapImage = img;
        };
    img.BeginInit();
    img.UriSource = new Uri("http://www.gravatar.com/avatar/c35af79e54306caedad37141f13de30c?s=128&d=identicon&r=PG");
    img.EndInit();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!