2012-01-19 125 views
1

我想獲得一個ObservableCollection的BitmapSource對象綁定到我的WPF窗體上的一堆圖像,並且該圖像永遠不會顯示...我驗證了圖像正在加載到屬性中,但我的綁定必須是不正確的......綁定路徑應該如何編碼?我曾經讓每個圖像綁定到一堆不同的對象,但是列表使用起來更好,所以我想用這種方式綁定它們......將圖像控件綁定到可觀察的集合

文本框正確顯示ProgramPath屬性,I就不能得到上述圖像源的約束

XAML - 內網格我有很多文本框和圖像的這樣

public class ExternalProgramsWindowData : INotifyPropertyChanged 
{ 
    private BitmapSource _ExtractPathImage(string fullPath) 
    { 
     BitmapSource returnedImage = null; 

     string pathForImage = string.Empty; 
     string[] s = fullPath.Split(new string[] { ".exe" }, StringSplitOptions.None); 

     if (s[0] != null) 
     { 
      pathForImage = s[0] + ".exe"; 
     } 

     if (!string.IsNullOrWhiteSpace(pathForImage)) 
     { 
      System.Drawing.Icon icon = IconExtractor.GetIcon(pathForImage, true); 

      returnedImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
       icon.Handle, 
       System.Windows.Int32Rect.Empty, 
       System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 
     } 

     return returnedImage; 

    } 

    /// <summary> 
    /// A 
    /// </summary> 
    private string _programPath; 
    public string ProgramPath 
    { 
     get { return _programPath; } 
     set 
     { 
      _programPath = value; 
      Notify("ProgramPath"); 
      ProgramImage = _ExtractPathImage(_programPath); 
     } 
    } 

    private BitmapSource _programImage; 
    public BitmapSource ProgramImage 
    { 
     get { return _programImage; } 
     set 
     { 
      _programImage = value; 
      Notify("ProgramImage"); 
     } 
    } 


    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void Notify(string propName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 

    #endregion 
} 
相互

<TextBox HorizontalAlignment="Stretch" Margin="24,2,2,2" Name="TextBoxA" VerticalAlignment="Stretch" 
          Width="664" > 
        <Binding Path=".[0].ProgramPath" UpdateSourceTrigger="PropertyChanged"> 
         <Binding.ValidationRules> 
          <local:ExternalProgramValidator/> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox> 
       <TextBox Grid.Row="1" HorizontalAlignment="Stretch" Margin="24,2,2,2" Name="TextBoxB" VerticalAlignment="Stretch" Width="664" > 
        <Binding Path=".[1].ProgramPath" UpdateSourceTrigger="PropertyChanged"> 
         <Binding.ValidationRules> 
          <local:ExternalProgramValidator/> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox> 

<Image Height="16 " HorizontalAlignment="Left" 
         Margin="4" Name="ImageA" Stretch="Fill" VerticalAlignment="Center" Width="16"       
         Source="{Binding Path=.[0].ProgramImage, UpdateSourceTrigger=PropertyChanged}"> 
       </Image> 
       <Image Grid.Row="1" Height="16 " HorizontalAlignment="Left" 
         Margin="4" Name="ImageB" Stretch="Fill" VerticalAlignment="Center" Width="16" 
         Source="{Binding Path=.[0].ProgramImage, UpdateSourceTrigger=PropertyChanged}"/> 

然後,我有一個公共類旁邊

在我網格綁定到那些類的集合主窗口類對象

/// <summary> 
/// Interaction logic for Window1.xaml 
/// </summary> 
public partial class ExternalProgramsWindow : Window 
{ 


    public ObservableCollection<ExternalProgramsWindowData> WindowDataList { get; set; } 


WindowDataList = new ObservableCollection<ExternalProgramsWindowData>(); 

     ExternalPrograms_ExternalProgramsGrid.DataContext = WindowDataList; 

然後我加載收集和與ProgramPath屬性被設置和它觸發設置ProgramImage(其被設定爲圖像正確,但窗口不顯示圖像)

foreach (ExternalProgram program in externalProgramList) 
     { 
      ExternalProgramsWindowData oExternalProgramsWindowData = new ExternalProgramsWindowData(); 
      oExternalProgramsWindowData.ProgramPath = program.Path + " " + program.Arguments; 


      WindowDataList.Add(oExternalProgramsWindowData); 
+0

[裝訂錯誤](http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/debugging-data-bindings-in-a-wpf - 或Silverlight的application.aspx)? –

+0

用更多代碼查看更新的問題...... – theDoke

+0

您仍然沒有提及綁定錯誤,並且格式化現在又很糟糕:( –

回答

0

試圖通過從它繼承使用自定義類作爲的ObservableCollection。在上面的代碼中,我看不到oberservable集合與要綁定的屬性之間的鏈接。

// assuming ExternalProgramsWindowData are your bitmap objects 
public class SourceList : ObservableCollection<ExternalProgramsWindowData> { 

} 

public class ViewModel : INotifyPropertyChanged { 

    private SourceList mySourceList; 

    public SourceList MySourceList { 
     get { return mySourceList; } 
     set { 
      mySourceList = value; 
      FirePropertyChanged("MySourceList"); 
      } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void FirePropertyChanged (string propertyName) { 

     if (PropertyChanged != null) { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

} 

然後在實際綁定你有以下情況:綁定的

來源:對象視圖模型。 綁定的路徑:「MySourceList」。

我不熟悉ExternalProgramsWindowData數據,但也許你必須使用ValueConverter來使綁定工作。

所以請注意,當您的viewmodels數據與您的Views數據不兼容時,儘管綁定路徑設置正確,但綁定將不起作用。

希望這有助於:)

+0

該列表設置爲DataContext,因此它不會顯示爲路徑中的屬性。也沒有必要具體繼承。 –

+0

查看更新後的問題用更多的代碼...... – theDoke

相關問題