WPF图像控制中的初始图像 [英] initial image in WPF Image Control

查看:146
本文介绍了WPF图像控制中的初始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF图像控件在我的项目中加载从互联网(懒加载),我想要显示初始图像在图像控制,直到主图像加载。 plz帮助我

 < DataTemplate DataType ={x:Type local:MyData}> 
...
< Image Width =50Height =50Source ={Binding Path = profile_image_url_https,FallbackValue = profile_image_url_https}Horizo​​ntalAlignment =Left>
...
< / DataTemplate>


解决方案

您可能可以使用 TargetNullValue 绑定,只有在加载时才设置image属性。



例如

 < BitmapImage x:Key =DefaultImageUriSource =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();
}


I have a WPF Image Control in my project that loads from internet (lazy loading), i want to show a initial image in Image Control until main image load. plz help me

<DataTemplate DataType="{x:Type local:MyData}">
...
 <Image Width="50" Height="50" Source="{Binding Path=profile_image_url_https, FallbackValue=profile_image_url_https}"  HorizontalAlignment="Left">
...
</DataTemplate>

解决方案

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();
}

这篇关于WPF图像控制中的初始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆