2013-01-16 40 views
4

我一直在瀏覽不同的帖子,試圖找出我的問題出了什麼問題。基本上我在我的用戶控件上有一個Image標記,並且我想綁定到一個url。但是這不起作用。我曾嘗試使用ValueConverter返回BitmapImage(new Uri((string)value)); 但這不起作用。我唯一能夠得到的是,你不能綁定到一個URL,你必須下載你想要綁定的圖像。我不想下載所有圖像我seacrch。是否有解決方案來完成這項任務,而無需在本地下載圖像。我認爲通過返回一個BitmapImage,ValueConverter方法會是最好的。請幫忙?將WPF中的圖像源綁定到Url

public class MyViewModel 
{ 
    private string _posterUrl; 
     public string PosterUrl 
     { 
      get 
      { 
       //Get Image Url, this is an example and will be retrieved from somewhere else. 
       _posterUrl = "http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg"; 
       return _posterUrl;  
      } 
      set 
      { 
       _posterUrl = value; 
       NofityPropertyChanged(p => p.PosterUrl); 
      } 
     } 
} 

這是我ValueConverter:

public class BitmapImageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if(value is string) 
      return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute)); 

     if(value is Uri) 
      return new BitmapImage((Uri)value); 

     throw new NotSupportedException(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

這是我的XAML:

<Image Source="{Binding PosterUrl, Converter={StaticResource bitmapImageConverter}}" Width="100" Height="100" /> 

所以這是綁定到包含IMAGEURL的PosterUrl財產,這是轉化爲BitmapImage的。有任何想法嗎?

+0

請在上面找到我的代碼 – Donny

+0

這應該都是完美的。也許對該URL的請求不會傳遞您的Web代理。你可以在Internet Explorer中檢索圖像嗎? – Clemens

+0

幸運的是,我沒有代理,它在IE中彈出 – Donny

回答

0

你想要做這個嗎?背後

<UserControl x:Class="WpfApplication1.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<StackPanel Orientation="Vertical"> 
    <Image Height="100" Width="100" Source="{Binding}" /> 

    <Image Height="100" Width="100" Source="http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg"/> 
</StackPanel> 

代碼:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     this.DataContext = "http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg"; 
     InitializeComponent(); 
    } 
} 
+0

請看我上面的代碼,我編輯了我的問題。 – Donny

1

試試吧

<Image Helpers:ImageAsyncHelper.SourceUri="{Binding Url, IsAsync=True}" x:Name="img" /> 

using System; 
using System.Windows; 
using System.Windows.Data; 
using System.Windows.Controls; 

public class ImageAsyncHelper : DependencyObject { 
    public static Uri GetSourceUri(DependencyObject obj){ 
     return (Uri)obj.GetValue(SourceUriProperty); 
    } 

    public static void SetSourceUri(DependencyObject obj, Uri value){ 
     obj.SetValue(SourceUriProperty, value); 
    } 

    public static readonly DependencyProperty SourceUriProperty = 
     DependencyProperty.RegisterAttached("SourceUri", 
      typeof(Uri), 
      typeof(ImageAsyncHelper), 
      new PropertyMetadata { PropertyChangedCallback = (obj, e) => 
       ((Image)obj).SetBinding(
        Image.SourceProperty, 
        new Binding("VerifiedUri"){ 
         Source = new ImageAsyncHelper{ 
          _givenUri = (Uri)e.NewValue 
         }, 
         IsAsync = true 
        } 
       ) 
      } 
     ); 

    private Uri _givenUri; 
    public Uri VerifiedUri { 
     get { 
      try { 
       System.Net.Dns.GetHostEntry(_givenUri.DnsSafeHost); 
       return _givenUri; 
      } 
      catch (Exception) { 
       return null; 
      } 
     } 
    } 
} 

public Uri Url { 
    get { 
     return new Uri(SomeString, UriKind.Absolute); 
    } 
}