2017-04-17 3 views
0

私には2つの値、文字列と画像が表示されるはずのコンボボックスがあります。私はComboBoxに追加したオブジェクトのリストを持っています。これらのオブジェクトには文字列とビットマップフィールドがあります。 ComboBoxに文字列を表示することはできますが、ビットマップを表示する方法が見つかりません。ビットマップオブジェクトをコンボボックスで表示する

<Image/>に私のビットマップを表示する方法はありますか?

私が知っているComboBox XAMLコードは動作しませんが、それを行う別の方法は考えられません。関連している私のフォームウィンドウの

<ComboBox x:Name="typeBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,318,0,0" VerticalAlignment="Top" Width="300" Height="29"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition></ColumnDefinition> 
         <ColumnDefinition></ColumnDefinition> 
        </Grid.ColumnDefinitions> 
        <TextBlock Text="{Binding name}" Grid.Column="0" Grid.Row="0"/> 
        <Image Width="20" Height="20" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="1" Source="{Binding img}"/> 
       </Grid> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 

C#コード:私のリソースの

public NewRes() 
{ 
     pc = new PicChanger(); 
     InitializeComponent(); 
     typeBox.DataContext = GlowingEarth.types; 
     tagBox.DataContext = GlowingEarth.tags; 
     if (GlowingEarth.types.Count > 0) 
     { 
      foreach (Model.Type t in GlowingEarth.types) 
      { 
       typeBox.Items.Add(t); 
      } 
     } 
     if (GlowingEarth.tags.Count > 0) 
     { 
      foreach (Model.Etiquette t in GlowingEarth.tags) 
      { 
       tagBox.Items.Add(t); 
      } 
     } 
} 

C#コード(タイプとエチケット)関連しているクラス:

public class Type 
{ 
    private string mark; 
    public string name { get; set; } 
    private string desc; 
    private Bitmap img; 

    public Type(string m, string n, string d, Bitmap mg) 
    { 
     mark = m; 
     name = n; 
     desc = d; 
     img = mg; 
    } 
} 

public class Etiquette 
{ 
    private string mark; 
    public string colCod { get; set; } 
    private string desc; 

    public Etiquette(string m, string c, string d) 
    { 
     mark = m; 
     colCod = c; 
     desc = d; 
    } 
} 

答えて

1

あなたのために十分なことがあります

Load a WPF BitmapImage from a System.Drawing.Bitmap

を...と公共財産を通じてBitmapImageを公開:

public class Type 
{ 
    private string mark; 
    public string name { get; set; } 
    private string desc; 
    private Bitmap bitmap; 
    public BitmapImage img { get; set; } 

    public Type(string m, string n, string d, Bitmap mg) 
    { 
     mark = m; 
     name = n; 
     desc = d; 
     bitmap = mg; 
     img = Convert(mg); 
    } 

    private static BitmapImage Convert(Bitmap bitmap) 
    { 
     BitmapImage bitmapImage; 
     using (System.IO.MemoryStream memory = new System.IO.MemoryStream()) 
     { 
      bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png); 
      memory.Position = 0; 
      bitmapImage = new BitmapImage(); 
      bitmapImage.BeginInit(); 
      bitmapImage.StreamSource = memory; 
      bitmapImage.CacheOption = BitmapCacheOption.OnLoad; 
      bitmapImage.EndInit(); 
     } 
     return bitmapImage; 
    } 
} 

あなたのバインディングは機能するはずです。 System.Drawing.Bitmapに直接バインドすることはできません。

+0

これを確認します。ありがとうございました!投票した:) – Riki

関連する問題