2011-11-01 56 views
1

我已經成功地將存儲在獨立存儲中的圖像綁定到XAML中定義的列表框中的「圖像」元素,而不會出現任何問題。圖像綁定,Silverlight,C#,WP7

我所做的是使用一個屬性,它將圖像的名稱和位置存儲在獨立存儲中,並使用IValueConverter實際打開文件並獲取其流。

我很驚訝地看到,我無法在新的XAML中使用更簡單的設置。我所擁有的是這樣的:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid.Resources> 
      <c:BinaryToImageSRCConverter x:Key="imageConverter" /> 
     </Grid.Resources> 
     <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill"> 
     </Image> 
    </Grid> 

,這就是我在模型視圖:

 public string PhotoName { get; set; } 
     PhotoName = "Images\\0.jpg"; 

文件的路徑和名稱是否正確因此它不是「沒有找到問題文件」。

我的問題是我的IValueConverter的Convert方法永遠不會被調用。

正如我之前提到的,我在另一個XAML(在同一個項目中)中成功使用了此轉換器,並且一切正常。唯一的區別是我綁定到ListBox使用的圖像。

任何想法或提示爲什麼我的IValueConverter的Convert方法沒有在這種情況下被調用?

謝謝!

這是我曾嘗試:

  1. 實施INotifyPropertyChanged的

    public class PhotoNameClass : INotifyPropertyChanged 
    { 
    public string PhotoNameValue = string.Empty; 
    
    public event PropertyChangedEventHandler PropertyChanged; 
    
    private void NotifyPropertyChanged(String info) 
    { 
        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, new PropertyChangedEventArgs(info)); 
        } 
    } 
    
    public string PhotoName 
    { 
        get 
        { 
         return this.PhotoNameValue; 
        } 
        set 
        { 
         if (value != this.PhotoNameValue) 
         { 
          this.PhotoNameValue = value; 
          NotifyPropertyChanged("PhotoName"); 
         } 
        } 
    } 
    
    } 
    

這是我賦予屬性:

public partial class ShowContent : PhoneApplicationPage 
{ 
    PhotoNameClass photoClass = new PhotoNameClass(); 
} 
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     photoClass.PhotoName = "Images\\0.jpg"; 
     base.OnNavigatedTo(e); 
    } 
  1. 使用的DependencyProperty

    public partial class ShowContent : PhoneApplicationPage 
    

    { // PhotoNameClass photoClass =新PhotoNameClass();

    public string PhotoName 
    { 
        get { return (string)GetValue(PhotoNameProperty); } 
        set { SetValue(PhotoNameProperty, value); } 
    } 
    
    public static readonly DependencyProperty PhotoNameProperty = DependencyProperty.Register("PhotoName", typeof(string), typeof(ShowContent), new PropertyMetadata("")); 
    
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
        PhotoName = "Images\\0.jpg"; 
    } 
    

這裏是XAML的相關部分:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid.Resources> 
      <c:BinaryToImageSRCConverter x:Key="imageConverter" /> 
     </Grid.Resources> 
     <Image Name="TheImage" Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill"> 
     </Image> 
    </Grid> 

其中,C被定義爲:

xmlns:c="clr-namespace:ImageApplication" 

感謝。

UPDATE

我使用INotifyPropertyChanged的實施,露出我的財產。

我已經改變了XAML代碼這樣:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid.Resources> 
      <c:PhotoNameClass x:Key="photoClass" /> 
      <c:BinaryToImageSRCConverter x:Key="imageConverter" /> 
     </Grid.Resources> 
      <Image Source="{Binding PhotoName, Converter={StaticResource imageConverter}}" DataContext="{StaticResource photoClass}" Width="430" Height="430" VerticalAlignment="Center" Stretch="UniformToFill"> 
      </Image> 
    </Grid> 
</Grid> 

因此,我添加了圖像元素指定圍繞我的屬性的類的名稱「的DataContext」屬性。在此場景中,IValueConverter的Convert方法IS被稱爲。然而,這是不正確的,因爲我在添加DataContext時遇到了XAML中的錯誤:「無法確定調用者的應用程序身份」,當然,進入Convert方法時,我的「值」字符串爲空,因此「正確「」PhotoName「沒有被填充。

感謝您的任何想法...

P.S.以下是我定義的IValueConverter我的轉換方法:

namespace ImageApplication 
{ 
    public class BinaryToImageSRCConverter : IValueConverter 
    { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { // <-- Breakpoint here. Never gets triggered except after I added the "DataContext" attribute to the XAML image element. 
     // but in that case, "value" is an empty string. 
     if (value != null && value is string) 
     { 
       ... 

更新修復

我有固定的問題。我已將包裝屬性的類名分配給XAML代碼中定義的圖像的DataContext屬性。

謝謝大家。我希望這會幫助那裏的人。

+0

我很困惑。你是說,完全相同的XAML在ListBox之外工作正常,但不在其中? – Gabe

+0

嗨Gabe,我說我在同一個項目中有另一個XAML,它使用與上面顯示的相同的「綁定語法」,並且在那裏一切正常。唯一的區別是該特定的XAML使用列表框中的圖像元素。 – MariusVE

+0

也提供轉換器代碼,如果你願意?有可能會有幫助 –

回答

0

它永遠不會被調用,因爲您的財產不會引發PropertyChanged通知。您需要將其設置爲DependencyProperty或實施INotifyPropertyChanged,並在設置屬性值時引發PropertyChanged事件。

+0

嗨Ed。我已經實現INotifyPropertyChanged,但仍然沒有調用我的Converter。我使用調試器只是爲了確保我的實現是正確的,它似乎是正確的,因爲當我分配給屬性名稱時,我可以確認使用屬性名稱調用了「NotifyPropertyChanged」。 – MariusVE

+0

@MariusVE:你應該告訴我們你的實際代碼。 –

+0

Ed,我已經修改了這個問題,以顯示我提出的解決方案的實現。謝謝。 – MariusVE