2017-04-17 18 views
0

我有一個組合框應該顯示兩個值,一個字符串和一個圖片。我列出了一些添加到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

您應該將System.Drawing.Bitmap對象轉換爲System.Windows.Media.Imaging.BitmapImage:通過公開財產

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

相關問題