Note that there are some explanatory texts on larger screens.

plurals
  1. POBindingError 40 on DataGrid CellEditingTemplate
    primarykey
    data
    text
    <p>I have a strange bug with my <code>DataTemplate</code> "<code>PersonDataTemplate</code>" I use it as <code>CellTemplate</code> and as<code>CellEditingTemplate</code>. On my <code>CellTemplate</code> all works fine but on my <code>CellEditingTemplate</code> I get following Error</p> <pre><code>System.Windows.Data Error: 40 : BindingExpression path error: 'PersonId' property not found on 'object' ''DataRowView' (HashCode=59318978)'. BindingExpression:Path=PersonId; DataItem='DataRowView' (HashCode=59318978); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'PersonName' property not found on 'object' ''DataRowView' (HashCode=59318978)'. BindingExpression:Path=PersonName; DataItem='DataRowView' (HashCode=59318978); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') </code></pre> <p>My Template</p> <pre><code> &lt;DataTemplate x:Key="PersonDataTemplate" DataType="Person"&gt; &lt;StackPanel&gt; &lt;TextBlock Background="LightBlue" Text="{Binding PersonId}"/&gt; &lt;TextBlock Background="AliceBlue" Text="{Binding PersonName}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>and here is the rest of my Code</p> <p><strong>Person.cs</strong></p> <pre><code> public class Person { private int personId; private string personName; public int PersonId { get { return personId; } set { personId = value; } } public string PersonName { get { return personName; } set { personName = value; } } } </code></pre> <p><strong>MainWindow.cs</strong></p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); simpleDataGrid.ItemsSource = LoadDataTable().AsDataView(); } /// &lt;summary&gt; /// Here i place my PersonDataTemplate as CellTemplate and CellEditingTemplate /// &lt;/summary&gt; private void simpleDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyType == typeof(Person)) { MyDataGridTemplateColumn col = new MyDataGridTemplateColumn(); col.ColumnName = e.PropertyName; col.CellTemplate = (DataTemplate)FindResource("PersonDataTemplate"); col.CellEditingTemplate = (DataTemplate)FindResource("PersonDataTemplate"); e.Column = col; e.Column.Header = e.PropertyName; } } /// &lt;summary&gt; /// Here I create and fill my DataTable /// &lt;/summary&gt; private DataTable LoadDataTable() { var _simpleDataTable = new DataTable(); var person = new DataColumn("Person") { DataType = typeof(Person) }; _simpleDataTable.Columns.Add(person); var dr1 = _simpleDataTable.NewRow(); dr1[0] = new Person { PersonId = 1, PersonName = "TONY" }; _simpleDataTable.Rows.Add(dr1); var dr2 = _simpleDataTable.NewRow(); dr2[0] = new Person { PersonId = 2, PersonName = "MAL" }; _simpleDataTable.Rows.Add(dr2); _simpleDataTable.AcceptChanges(); return _simpleDataTable; } private void simpleDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) { // just to check the Values } } public class MyDataGridTemplateColumn : DataGridTemplateColumn { public string ColumnName { get; set; } protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { // The DataGridTemplateColumn uses ContentPresenter with your DataTemplate. ContentPresenter cp = (ContentPresenter)base.GenerateElement(cell, dataItem); // Reset the Binding to the specific column. The default binding is to the DataRowView. BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding(this.ColumnName)); return cp; } } </code></pre> <p><strong>MainWindow.XAML</strong></p> <pre><code>&lt;Window x:Class="HowBindDataTableToDataGrid.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;Window.Resources&gt; &lt;!-- you for each Class one DataTemplate--&gt; &lt;DataTemplate x:Key="PersonDataTemplate" DataType="Person"&gt; &lt;StackPanel&gt; &lt;TextBlock Background="LightBlue" Text="{Binding PersonId}"/&gt; &lt;TextBlock Background="AliceBlue" Text="{Binding PersonName}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;DataGrid Name="simpleDataGrid" AutoGeneratingColumn="simpleDataGrid_AutoGeneratingColumn" BeginningEdit="simpleDataGrid_BeginningEdit" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>Special Information's</strong></p> <ul> <li>My ItemsSource is a DataTable</li> <li>My <code>MyDataGridTemplateColumn</code> based on <a href="https://stackoverflow.com/a/5355195/1993545">this</a> Answer (I also tested the other solution with no luck)</li> <li>I also tested a separate DataTemplate for my <code>CellEditingTemplate</code> with no luck</li> </ul>
    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.
 

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