2013-11-22 47 views
1
public class Thumbnail : INotifyPropertyChanged 
    { 
     public BitmapImage testimage { get; set; } 
     Thumbnail() 
     { 
      testimage = new BitmapImage(new Uri("http://www.diseno-art.com/news_content/wp-content/uploads/2012/09/2013-Jaguar-F-Type-1.jpg")); 
     } 
    } 

而且在自定義控制圖像元素:結合的BitmapImage

<Image x:Name="previewImage" x:FieldModifier="public" Margin="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="{Binding Path=thumbnail.testimage}" Stretch="Fill" /> 

當我創建一個控制,我這樣做:

MyControl MC = new MyControl(); 
MC.DataContext = new Thumbnail(); 

和圖像不顯示 - 爲什麼?

回答

2

XAML:

<Image x:Name="previewImage" x:FieldModifier="public" Margin="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="{Binding Path=testimage}" Stretch="Fill" /> 

代碼(清潔劑):

public class Thumbnail : INotifyPropertyChanged 
     { 
      private BitmapImage tmpbmp; 
      public BitmapImage testimage { get { return tmpbmp; } set { tmpbmp = value; OnPropertyChanged("testimage"); } } 
      public Thumbnail() 
      { 
       tmpbmp = new BitmapImage(new Uri("http://www.diseno-art.com/news_content/wp-content/uploads/2012/09/2013-Jaguar-F-Type-1.jpg")); 
      } 


      public event PropertyChangedEventHandler PropertyChanged; 

      protected virtual void OnPropertyChanged(string propertyName) 
      { 
       PropertyChangedEventHandler handler = PropertyChanged; 
       if (handler != null) 
        handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
0

縮略圖類沒有公共構造方法,不執行INotifyPropertyChanged成員(根本不需要在你的例子)。和XAML應該是:

<Image x:Name="previewImage" x:FieldModifier="public" Margin="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="{Binding Path=testimage}" Stretch="Fill" />