The bidirectional binding requires “Path” or “XPath” exception

Jim Jupiter 66 Reputation points
2021-07-20T20:08:24.84+00:00

Hi

exception occurs when I leave a cell in my datagrid - but I didn't know where to find the error

<DataGrid ItemsSource="{Binding}" ...


<DataGrid.Columns>
                <DataGridTextColumn Width="40" Header="ID" Binding="{Binding ID}" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>

                <DataGridTextColumn Width="250" Header="Name" Binding="{Binding Name}" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>

My xaml looks like this ... and this is code behind

  InitializeComponent();
    dGrid.DataContext = DT.Datatable;

Any Idea?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,770 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,531 Reputation points Microsoft Vendor
    2021-07-21T09:58:18.703+00:00

    I used your code in xmal and the background code uses the following code . Exception does not occur when I leave a cell in my datagrid. Did I do something less or is it different from yours? Please show me more relevant steps and codes to reproduce and analyze the problem.
    The code of xaml is as follows:

    <Grid>  
            <DataGrid x:Name="dGrid" ItemsSource="{Binding}" AutoGenerateColumns="False"  >  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="ID" Width="80" Binding="{Binding ID}"  >  
                        <DataGridTextColumn.ElementStyle>  
                            <Style TargetType="TextBlock">  
                                <Setter Property="HorizontalAlignment" Value="Center" />  
                            </Style>  
                        </DataGridTextColumn.ElementStyle>  
                    </DataGridTextColumn>  
                    <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}">  
                        <DataGridTextColumn.ElementStyle>  
                            <Style TargetType="TextBlock">  
                                <Setter Property="HorizontalAlignment" Value="Center" />  
                            </Style>  
                        </DataGridTextColumn.ElementStyle>  
                    </DataGridTextColumn>  
                </DataGrid.Columns>  
            </DataGrid>  
      </Grid>  
    

    The code of xaml.cs is as follows:

    using System.Data;  
    using System.Windows;  
    namespace BidirectionalBindingException  
    {  
      public partial class MainWindow : Window  
      {   
        public MainWindow()  
        {  
          InitializeComponent();  
          dGrid.DataContext = GetDummyData();  
        }  
        private static DataTable GetDummyData()  
        {  
          DataTable dt = new DataTable();  
          DataColumn ID = new DataColumn("ID", typeof(int));  
          dt.Columns.Add(ID);  
          dt.Columns.Add("Name", typeof(string));  
          dt.PrimaryKey = new DataColumn[] { ID };  
          dt.Rows.Add(1, "Name1");  
          dt.Rows.Add(2, "Name2");  
          return dt;  
        }  
      }  
    }  
    

    The result is shown in the figure:
    116734-22.gif

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jim Jupiter 66 Reputation points
    2021-07-21T18:08:37.173+00:00

    Aaarg - I just wanted to post more - then the error blinks in my eyes :))

    Binding line in column 3

    <DataGridTextColumn Width="250" Header="Visual" Binding="{Binding VS}" >

    DT.Columns.Add("Vs", typeof(string));

    Mistyping Name

    thanks for your help

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.