HOW TO BIND THE LOCAL IMAGE IN DATAGRID USING WPF?

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
WPF 
WPF
 5671  View(s)
Ratings:
Rate this:

I have a datagrid in wpf and I want to show an image in image column. How to do the task?

Please provide a demo if possible.

Any help will be appreciated.

  1. shreesh chandra shukla

    Post:105

    Points:735
    Re: How to bind the local image in datagrid using wpf?

    Hi!

    You can use the below lines of code to show an image in image column of datagrid.
    In window.xaml code :-
            <DataGrid AutoGenerateColumns="False" Margin="12" Name="dataGrid1">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding ImageFilePath}" Width="50" Height="50" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
    In window.xaml.cs code :-
    public class imageBinding
            {
                public BitmapImage ImageFilePath { get; set; }
            }
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i <= 20; i++)
                {
                    dataGrid1.Items.Add(new imageBinding { ImageFilePath = new BitmapImage(new Uri(@"Physical Image File Path           ")) });
                }
            }
    thanks!

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!