2011-12-21 15 views
2

私のWPFアプリケーションでは、jpegファイルを動的に作成しています。 bin/Debugまたはbin/Releaseに保存されています。WPFで動的に作成されたjpegをデータバインド

私はそうのようなValueConverterを使用しているイメージコントロールとデータバインドさWrapPanelを持っている:

​​

値の値の例は、私はビン\デバッグやビン\リリースに見て、デフォルトであると仮定Images\400\26.jpgです。

私の問題は、ダイナミックに作成されたjpegにイメージコントロールをデータバインドすることができないようです。しかし私はIncludeとマークし、BuildActionContentである他のjpegにデータバインドすることができます。

コンパイル時に存在しない動的に作成されたイメージにどのようにデータバインドするのですか?

+0

あなたが動的に作成されたイメージのリストビューが表示リストを取得します。この時点で

 private void dynamicallyJpegCreator(string newImagePath) { // Creating Image code // ..... // Create new image at newImagePath m_viewModel.Add(new System.Uri(newImagePath, System.UriKind.RelativeOrAbsolute)); } 

最後に、我々はあなたの動的JPEGクリエーター機能を変更することができますプロジェクトに含まれるファイルに限定されています。 イメージmyImage3 = new Image(); BitmapImage bi3 =新しいBitmapImage(); bi3.BeginInit(); bi3.UriSource = new Uri( "smiley_stackpanel.PNG"、UriKind.Relative); bi3.EndInit(); myImage3.Stretch =ストレッチ。塗りつぶし; myImage3.Source = bi3; – Paparazzi

答えて

4

イメージパスのコレクションを作成し、新しく作成したイメージのパスを追加することができます。これであなたのラッパエルをこのコレクションにバインドすることができます。

このコレクションをViewModelクラスに実装し、このViewModelクラスをDataContextにして、コンバータを使用してwrapPanelをそのコレクションにバインドすることができます。

編集:ここではサンプルファイルは、次のとおりです。

MainWindowViewModel.cs:

using System; 
using System.ComponentModel; 
using System.Collections.ObjectModel; 

namespace wpfJpegBindingSample 
{ 
    class MainWindowViewModel : INotifyPropertyChanged 
    { 
     #region INotifyPropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 

     // Create the OnPropertyChanged method to raise the event 
     protected void OnPropertyChanged(string name) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 
      } 
     } 
     #endregion 

     //constructor 
     public MainWindowViewModel() 
     { 
      m_imagesList = new ObservableCollection<Uri>(); 
     } 

     //collection of images' Uris 
     private ObservableCollection<Uri> m_imagesList; 

     //property for the collection (so you can bind to it) 
     public ObservableCollection<Uri> ImagesList 
     { 
      get 
      { 
       return m_imagesList; 
      } 
     } 

     //an Add method that update the bindings 
     public void Add(Uri uri) 
     { 
      ImagesList.Add(uri); 
      OnPropertyChanged("ImagesList"); 
     } 

    } 
} 

MainWindow.xaml.cs:

using System.Windows; 

namespace wpfJpegBindingSample 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = m_viewModel = new MainWindowViewModel(); 
     } 

     private MainWindowViewModel m_viewModel; 
    } 
} 

がMainWindow.xaml:

<Window x:Class="wpfJpegBindingSample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ListView ItemsSource="{Binding ImagesList}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <WrapPanel> 
         <Image Source="{Binding}"/> 
        </WrapPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

    </Grid> 
</Window> 

ご覧のとおり、私たちは画像のウリスのコレクションを定義しました。 メインウィンドウのコードでは、そのビューモデルのオブジェクトをウィンドウデータコンテキストとして設定し、xamlではlistView(または選択した他のコントロール)のItemソースをImagesListプロパティのバインディングに設定しましたビューモデルであるデータコンテキスト。それはないかもしれないとして、あなたが背後にあるコードを試してみてください

+2

@リ:私はあなたが私の答えをupvoted見た。それはあなたを助けましたか?そうだった場合は、それを解決策としてマークしてください。 – Seffix

関連する問題