'How can I link Image Source = "?" with Value from DataTable (path image) in WPF?

I am looking to change the value Source = "img / Best / 1.png" for the DataGrid cell that I clicked on, with a click, a button, any option.

How can I bind the value in the DataGrid cell loaded from DataTable? I've tried different options and nothing worked. It is complicated WPF for a beginner.

Here is a concept photo:

Here is a concept photo

I have cross-posted this to the Russian Stack Overflow site.

Here's an example of implementing my idea in Windows Forms:

https://github.com/grebtsew/Patterns-Maker

<Window.Resources>
    <viewModel:DataRowViewConverter x:Key="drvc" />
    <DataTemplate x:Key="ATemplate">
        <Image x:Name="MyImage" Source="img/Best/1.png"></Image>
    </DataTemplate>
</Window.Resources>


<DataGrid x:Name="MyGrid" AutoGenerateColumns="True" ItemsSource="{Binding Items}" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" CanUserAddRows="True" SelectedCellsChanged="MyGrid_SelectedCellsChanged" SelectionUnit="Cell" Grid.ColumnSpan="2">
</DataGrid>
DataGrid MyGrid = new DataGrid();
System.Data.DataTable MyDataTable = new System.Data.DataTable();
MyDataTable.Columns.Add("Col1", typeof(string));
MyDataTable.Columns.Add("Col2", typeof(string));
MyDataTable.Columns.Add("Col3", typeof(string));
MyDataTable.Columns.Add("Col4", typeof(string));
MyDataTable.Columns.Add("Col5", typeof(string));
// dt.Columns.Add("2 тип А", typeof(A));

MyDataTable.Rows.Add("test", "img/Best/1.png", "img/Best/1.png", "img/Best/1.png");
MyDataTable.Rows.Add("TestString");

Items = MyDataTable;
this.DataContext = this;


Solution 1:[1]

I'll admit this is something of a fail for WPF, but you are almost there. The big problem is that as soon as you use a template column, it becomes hard to get hold of the column index, which you need to bind to the data table.

Anyway, along with your converter, it works if you make ATemplate:

<DataTemplate x:Key="ATemplate">
    <Image 
        DataContext="{
            Binding RelativeSource={
            RelativeSource AncestorType=DataGridCell},
            Converter={StaticResource drvc}}" 
        Source="{Binding}" />
</DataTemplate>

(Yuk! And yes, this is about as bad as WPF gets. It's a horrible introduction to the framework. If possible avoid autogenerated columns. Although I appreciate that this might not be possible, WPF is actually a good GUI framework).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen