Riki
Riki

Reputation: 341

Making a Bitmap object visible in combobox

I have a ComboBox that should show me two values, a string and a picture. I have a list of some objects that I added to the ComboBox. Those objects have the string and Bitmap fields. I can show the string in my ComboBox, but I can't find a way to show the Bitmap.

Is there any way to make <Image/> show my Bitmap?

Here is my ComboBox XAML code which I know isn't working, but I can't think of another way to do it.

<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# code of my form window that is relevant:

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# code of my resources (Type and Etiquette) classes that are relevant:

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;
    }
}

Upvotes: 0

Views: 344

Answers (2)

mm8
mm8

Reputation: 169190

You should convert your System.Drawing.Bitmap object to a System.Windows.Media.Imaging.BitmapImage:

Load a WPF BitmapImage from a System.Drawing.Bitmap

...and expose the BitmapImage through a public property:

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;
    }
}

Then your binding should work. You cannot bind directly to a System.Drawing.Bitmap.

Upvotes: 2

caner
caner

Reputation: 721

Check this out...

Image UriSource and Data Binding

and this answer may be enough for you https://stackoverflow.com/a/20648/2470530

Upvotes: 0

Related Questions