Search code examples
c#wpfwpfdatagrid

Set dynamic images to Autogenerated columns in WPF Datagrid


DataGrid is declared like that

<DataGrid x:Name="DgDirectories" 
                  AutoGenerateColumns="True" />

It is dynamically populated using DgDirectories.ItemSource property with IEnumerable of my DTO object. This object contains byte[] property with dynamic image (does not exist on HDD). I want to show those images in the grid.

I've tried to convert byte[] to BitmapImage and pass it with data but anyway grid creates DataGridTextColumn for those type.

Probably this can be done much easier but I have a small experience using WPF... Please help me to display images in column

P.S.: I'm newbie in WPF.


Solution

  • Xaml code for dynamic pathname binding

    <DataGrid x:Name="dataGrid1" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding ImageFilePath}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    now just start your code behind logic with uri object instantiated with IEnumerable list of file paths... Hope this would suffice