Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code to add a DataGridTemplateColumn programatically. </p> <p><a href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/df77a277-91d4-41f1-a42a-0fa02a443ff4" rel="noreferrer">http://social.msdn.microsoft.com/Forums/en/wpf/thread/df77a277-91d4-41f1-a42a-0fa02a443ff4</a></p> <pre><code>DataGridTemplateColumn imgColumn = new DataGridTemplateColumn(); imgColumn.Header = "Image"; FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image)); imageFactory.SetBinding(Image.SourceProperty, new Binding("ImgPath")); DataTemplate dataTemplate = new DataTemplate(); dataTemplate.VisualTree = imageFactory; imgColumn.CellTemplate = dataTemplate; DGImages.Columns.Add(imgColumn); </code></pre> <p>I have also added the source code of the sample app that I created. Hope it helps.</p> <p>MainWindow.xaml file</p> <pre><code>&lt;Window x:Class="StackOverflow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;DataGrid Name="DGImages" AutoGenerateColumns="False"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Image Desc" Binding="{Binding ImgDesc}"&gt;&lt;/DataGridTextColumn&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>MainWindow.xaml.cs file</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel; namespace StackOverflow { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { private ObservableCollection&lt;SampleClass&gt; imgList = null; public MainWindow() { InitializeComponent(); imgList = new ObservableCollection&lt;SampleClass&gt;(); imgList.Add(new SampleClass() { ImgDesc = "First Image", ImgPath = @"/Images/MyImage.jpg" }); DataGridTemplateColumn imgColumn = new DataGridTemplateColumn(); imgColumn.Header = "Image"; FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image)); imageFactory.SetBinding(Image.SourceProperty, new Binding("ImgPath")); DataTemplate dataTemplate = new DataTemplate(); dataTemplate.VisualTree = imageFactory; imgColumn.CellTemplate = dataTemplate; DGImages.Columns.Add(imgColumn); this.DGImages.ItemsSource = imgList; } } } </code></pre> <p>SampleClass.cs file</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace StackOverflow { public class SampleClass : INotifyPropertyChanged { private string _ImgDesc; public string ImgDesc { get { return _ImgDesc; } set { _ImgDesc = value; OnPropertyChanged("ImgDesc"); } } private string _ImgPath; public string ImgPath { get { return _ImgPath; } set { _ImgPath = value; OnPropertyChanged("ImgPath"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload