2012-01-19 11 views
1

ObservableCollectionのBitmapSourceオブジェクトを取得して、WPFフォーム上の画像の束にバインドして、画像が表示されないようにしようとしています...画像がプロパティにロードされていることを確認しました。私のバインディングは間違っているはずです.....どのようにバインディングパスをコード化する必要がありますか?私は、それぞれのイメージをさまざまなオブジェクトに束縛していましたが、リストを使用するほうがはるかに優れているので、バインドしたいと思います。......画像コントロールを観察可能なコレクションにバインドする

テキストボックスにProgramPathプロパティが正しく表示されます。ちょうどイメージソースは

XAMLを拘束取得することはできません - グリッドの中に私はお互い

<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}"/> 

の隣のテキストボックスや画像の数を持っているそして、私はこの

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 
} 
のような公共のクラスを持っていますメインウィンドウクラスで210

私は、これらのクラスのコレクションにグリッドをバインドする

/// <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を-or-silverlight-application.aspx)? –

+0

更なるコードで更新された質問を見る...... – theDoke

+0

あなたはまだバインディングエラーについて言及していませんし、書式もまた恐ろしいものです:( –

答えて

0

はそれからを継承してのObservableCollectionとしてカスタムクラスを使用してみてください。上のコードでは、oberservableコレクションとバインドしたいプロパティの間のリンクは表示されません。バインディングの

出典::オブジェクトのViewModel

// 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

関連する問題