xeraphim
xeraphim

Reputation: 4645

Display an image in a DataGrid-Column

I'm trying to display an image in a DataGrid-Column next to other data. My model looks like this:

public class Person 
{
    public string Name { get; set; }
    public string Address { get; set; }
    public Bitmap Image { get; set; }
}

The ViewModel:

public ObservableCollection<Person> Persons
    {
        get
        {
           return this.persons;
        }

        set
        {
            this.persons = value;
        }
    }

And my DataGrid is this:

<DataGrid Name="Persons"
              Grid.Row="1"
              Grid.Column="0"
              Margin="10"
              AutoGenerateColumns="False"
              IsReadOnly="True"
              ItemsSource="{Binding Path=Persons}">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="80">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Width="120"
                                Binding="{Binding Path=Name}"
                                Header="Name" />
            <DataGridTextColumn Width="120"
                                Binding="{Binding Path=Address}"
                                Header="Address" />
        </DataGrid.Columns>
    </DataGrid>

The Name and Adress get displayed correctly, but the image is empty... Do you know what I'm doing wrong?

Thanks in advance and have a nice day

Upvotes: 0

Views: 13429

Answers (6)

M W
M W

Reputation: 36

At first you do bind your whole data object to the image not only the "image" of your data object.

Change your XAML code like this:

<Image Source="{Binding Path=Image}" />

Second: A XAML <Image Source="{Binding Path=Image}" /> does expect an ImageSource or a BitmapSource as data not a Windows Bitmap. You have to cast your Bitmap to a BitmapSource and have to change your data object accordingly:

public class Person 
{
   public string Name { get; set; }
   public string Address { get; set; }
   public BitmapSource Image { get; set; }
}

Here is a working example to convert your Drawing.Bitmap to a BitmapSource:

public static BitmapSource 
 Convert(System.Drawing.Bitmap bitmap)
{
    var bitmapData = bitmap.LockBits(
    new System.Drawing.Rectangle(0, 0, bitmap.Width, 
        bitmap.Height),
                 
      System.Drawing.Imaging.ImageLockMode.ReadOnly, 
     bitmap.PixelFormat);

    var bitmapSource = BitmapSource.Create(
    bitmapData.Width, bitmapData.Height,
    bitmap.HorizontalResolution, 
     bitmap.VerticalResolution,
    PixelFormats.Bgr24, null,
    bitmapData.Scan0, bitmapData.Stride * 
    bitmapData.Height, bitmapData.Stride);

    bitmap.UnlockBits(bitmapData);

    return bitmapSource;}

Now you can load your Bitmap e.g from a file and convert it and bind it to your data object:

person = new Person()
person.Image =  Convert(new Bitmap("myimage.png"));

.....

Upvotes: 0

Fred
Fred

Reputation: 1

xmlns:converter="clr-namespace:ProjectName.Converters"
<Window.Resource>
 <converter:BindableConverter x:Key="bindableConverter"/>
</Window.Resource>    
<DataGridTemplateColumn Header="HeaderName">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Image x:Name="BindImg" Height="35" Width="35" Source="{Binding IsBindable,Converter={StaticResource bindableConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>                             
                            </DataGridTemplateColumn> 


 public class BindableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string imageSource = string.Empty;
            bool isBinded;
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                imageSource = "../../Resource/Images/unbinded.jpg";
            }
            if (Boolean.TryParse(value.ToString(), out isBinded))
            {
                if (isBinded)
                {
                    imageSource = "../../Resource/Images/binded.jpg";
                }
                else
                {
                    imageSource = "../../Resource/Images/unbinded.jpg";
                }
            }
            return imageSource;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Upvotes: 0

dkozl
dkozl

Reputation: 33394

If your Image is of a System.Drawing.Bitmap you should change it to System.Windows.Media.Imaging.BitmapImage and then change also Image.Source binding which at the moment binds to whole Person object to its Image property

<Image Source="{Binding Image}" />

Upvotes: 3

Harjeet Singh
Harjeet Singh

Reputation: 396

You need to set some Height and Width for the image like

<Image Source="{Binding}" Height="200" Width="200"/>

Upvotes: 0

MRebai
MRebai

Reputation: 5474

Try to use that code to display your image :

 <DataGridTemplateColumn Width="80">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Image Source="{Binding Image}" />
            </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>

Upvotes: 2

Vikas
Vikas

Reputation: 50

Add image floder in your project and add your images in the same folder. use following code to access

<Columns> 
<ItemTemplate >
<asp:ImageButton ID="ImageButton1" runat ="server" CommandName="Preview" ImageUrl="~/images/MSWord_Icon.jpg"/>
</ItemTemplate>
</Columns>

Upvotes: 0

Related Questions