2012-01-19 88 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

相关问题