如何修复“双向绑定需要 Path 或 XPath”问题WPF 数据网格中的异常?
此问题的背景/背景: 我有一个 WPF 桌面应用程序。它使用 LINQ to SQL 连接到其 SQL 数据库,并在 WPF Datagrids 中显示其数据。它工作得相当好,但性能是一个问题,因为 LINQ 可能非常慢,所以我一直尽可能地将逻辑和 UI 控件从 LINQ 数据库上下文中切换出来,而是将它们加载到与以下内容非常相似的局部变量中: LINQ 对象,这极大地提高了性能。
问题: 当我测试我的数据网格时,当我尝试编辑某个(整数)中的值时,我现在收到一个新的异常“双向绑定需要 Path 或 XPath。” )一个数据网格的列,尽管编辑字符串列一直工作正常。我不明白为什么我会得到这个,或者该怎么办。
因此,当 datagrid.datacontext 设置为 LINQ 实体关联时,它可以工作,但只有当它设置为普通对象列表时,它才几乎工作。我尝试将列表更改为 ObservableCollection,但这没有明显效果。
我在这里和其他网站上查看了大约十几个不同的相关问题,但没有看到任何似乎有帮助的内容。
目前我有:
<DataGrid Margin="12,110,12,0" x:Name="dgDays" ItemsSource="{Binding}"
Height="165" VerticalAlignment="Top" MinHeight="0"
AutoGenerateColumns="False"
SelectionChanged="dgDays_SelectionChanged">
...
<DataGrid.Columns>
<DataGridComboBoxColumn Width="80" Header="Cook" x:Name="_DailyCookCombo" SelectedItemBinding="{Binding sCook}"/>
<DataGridComboBoxColumn Width="80" Header="Eat" x:Name="_DailyDayCombo" SelectedItemBinding="{Binding sDay}"/>
<DataGridTextColumn Width="52" Header="Prot" Binding="{Binding Protein}" />
<DataGridTextColumn Width="52" Header="Carb" Binding="{Binding Carb}" />
<DataGridTextColumn Width="52" Header="Fat" Binding="{Binding Fat}" />
<DataGridTextColumn Width="62" Header="Prot %" Binding="{Binding ProteinPercent}" />
<DataGridTextColumn Width="62" Header="Carb %" Binding="{Binding CarbPercent}" />
<DataGridTextColumn Width="62" Header="Fat %" Binding="{Binding FatPercent}" />
<DataGridTextColumn Width="116" Header="non PFW meals" Binding="{Binding NonPFWMeals}" />
<DataGridTextColumn Width="55" Header="Prot" Binding="{Binding CalcProt}" IsReadOnly="True" />
<DataGridTextColumn Width="55" Header="Carb" Binding="{Binding CalcCarbs}" IsReadOnly="True" />
<DataGridTextColumn Width="55" Header="Fat" Binding="{Binding CalcFat}" IsReadOnly="True" />
<DataGridTextColumn Width="65" Header="KCal" Binding="{Binding CalcKCal}" IsReadOnly="True" />
<DataGridCheckBoxColumn Width="32" Header="Skip" Binding="{Binding Skip}" />
<DataGridTextColumn Width="70" Header="Date" Binding="{Binding sNextDate}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
由代码绑定:
dgDays.DataContext = TheMember.RAM_Member_Requirements_Days;
定义为:
public ObservableCollection<RAM_Member_Requirements_Day> RAM_Member_Requirements_Days = new ObservableCollection<RAM_Member_Requirements_Day>();
其绑定成员是:
public class RAM_Member_Requirements_Day : INotifyPropertyChanged
{
// RAM equivalents of DB values:
public System.Nullable<decimal> Protein;
public System.Nullable<decimal> Carb;
public System.Nullable<decimal> Fat;
public System.Nullable<decimal> ProteinPercent;
public System.Nullable<decimal> CarbPercent;
public System.Nullable<decimal> FatPercent;
public System.Nullable<int> NonPFWMeals;
public System.Nullable<bool> Skip;
public System.Nullable<System.DateTime> SkipDate;
我在输入此内容后不久发现了一个非常简单的修复,当网站允许我在 8 小时后发布时,我将发布该修复延迟。
Background/context for this question:
I have a WPF desktop application. It uses LINQ to SQL to connect to its SQL database, and it displays its data in WPF Datagrids. It was working fairly well, but performance was a problem because LINQ can be deadly slow, so I have been switching my logic and UI controls away from LINQ database contexts as much as possible, and instead loading them into local variables which are very similar to the LINQ objects, which massively improves performance.
The problem:
As I test my Datagrids, I am now getting a new exception "Two-way binding requires Path or XPath." when I try to edit the value in a certain (integer) column of one Datagrid, though editing string columns had been working fine. I don't understand why I am getting this, or what to do about it.
So it worked when the datagrid.datacontext was set to a LINQ entity association, but it only almost works when it is set to a list of plain objects. I tried changing the list to an ObservableCollection, but this had no apparent effect.
I have looked at about a dozen different related questions here and on other sites, and am not seeing anything that seems to help.
Currently I have:
<DataGrid Margin="12,110,12,0" x:Name="dgDays" ItemsSource="{Binding}"
Height="165" VerticalAlignment="Top" MinHeight="0"
AutoGenerateColumns="False"
SelectionChanged="dgDays_SelectionChanged">
...
<DataGrid.Columns>
<DataGridComboBoxColumn Width="80" Header="Cook" x:Name="_DailyCookCombo" SelectedItemBinding="{Binding sCook}"/>
<DataGridComboBoxColumn Width="80" Header="Eat" x:Name="_DailyDayCombo" SelectedItemBinding="{Binding sDay}"/>
<DataGridTextColumn Width="52" Header="Prot" Binding="{Binding Protein}" />
<DataGridTextColumn Width="52" Header="Carb" Binding="{Binding Carb}" />
<DataGridTextColumn Width="52" Header="Fat" Binding="{Binding Fat}" />
<DataGridTextColumn Width="62" Header="Prot %" Binding="{Binding ProteinPercent}" />
<DataGridTextColumn Width="62" Header="Carb %" Binding="{Binding CarbPercent}" />
<DataGridTextColumn Width="62" Header="Fat %" Binding="{Binding FatPercent}" />
<DataGridTextColumn Width="116" Header="non PFW meals" Binding="{Binding NonPFWMeals}" />
<DataGridTextColumn Width="55" Header="Prot" Binding="{Binding CalcProt}" IsReadOnly="True" />
<DataGridTextColumn Width="55" Header="Carb" Binding="{Binding CalcCarbs}" IsReadOnly="True" />
<DataGridTextColumn Width="55" Header="Fat" Binding="{Binding CalcFat}" IsReadOnly="True" />
<DataGridTextColumn Width="65" Header="KCal" Binding="{Binding CalcKCal}" IsReadOnly="True" />
<DataGridCheckBoxColumn Width="32" Header="Skip" Binding="{Binding Skip}" />
<DataGridTextColumn Width="70" Header="Date" Binding="{Binding sNextDate}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
which is bound by the code:
dgDays.DataContext = TheMember.RAM_Member_Requirements_Days;
which is defined as:
public ObservableCollection<RAM_Member_Requirements_Day> RAM_Member_Requirements_Days = new ObservableCollection<RAM_Member_Requirements_Day>();
whose bound members are:
public class RAM_Member_Requirements_Day : INotifyPropertyChanged
{
// RAM equivalents of DB values:
public System.Nullable<decimal> Protein;
public System.Nullable<decimal> Carb;
public System.Nullable<decimal> Fat;
public System.Nullable<decimal> ProteinPercent;
public System.Nullable<decimal> CarbPercent;
public System.Nullable<decimal> FatPercent;
public System.Nullable<int> NonPFWMeals;
public System.Nullable<bool> Skip;
public System.Nullable<System.DateTime> SkipDate;
I found a very simple fix shortly after typing this, which I'll post when the site lets me after its 8-hour delay.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在绑定文本框时遇到了这个问题。我的解决方案是显式绑定到“.”:
这就成功了。
I ran into this problem with a bound textbox. My solution was to explicitly bind to ".":
That did the trick.
“双向绑定需要 Path 或 XPath”错误可能是由于 XAML 中的路径名称与 c# Binding 元素中的路径名称略有不同而导致的。大写字母真的很重要!那是我的问题。
XAML:
C#:
这将导致双向绑定需要 Path 或 XPath 错误,因为它们不完全相同,并且程序无法找到绑定路径!
也许它会帮助犯同样错误的其他人
A "Two-way binding requires Path or XPath" fault can be cause by a little difference in the name of the path in the XAML vs the one in the c# Binding element. The capitals letter are really important! That was my problem.
XAML:
C#:
this will cause a Two-way binding requiring a Path or XPath error because they are not exactly the same and the programs is not able to find the binding path!
Maybe it will help someone else who made the Same mistake
好吧,输入所有内容后,我尝试了一些有效的方法。无论如何我都会发帖,以防对其他人有帮助。
解决方案是向问题添加绑定的成员变量:
如下所示:
Ok, well, having typed all that in, I tried something which worked. I am posting anyway, in case it helps others.
The solution was to add to the problem member variables that are bound:
As in:
出现此错误的另一种方式(在我看来是错误的)。我有一个 DataGrid,其中 ItemsSource 行对象包含类对象 X。我在 X 的各个列中显示。
绑定路径看起来像 Binding="{Binding MyClassInstance.PropA}"
另外,网格设置为允许用户添加行。我忘记在行添加中执行的操作是安排 MyClassInstance 进行初始化,因此它为空。当我从新行中删除焦点时,我会得到双向绑定需要 Path 或 XPath 异常。
当我尝试修复导致我得到空值异常的方法之一时,我找到了真正的原因。
绑定需要路径错误的堆栈跟踪非常广泛,这告诉我开发人员一旦深入调用,就猜测空值最可能的原因是路径问题。就我个人而言,对于异常消息来说“遇到空值,检查是否指定了路径或 xpath”会更有用。
Yet another way for this error to crop up (erroneously in my opinion). I have a DataGrid where the ItemsSource row object contains a class object X. I display in individual columns from X.
The binding path looks like Binding="{Binding MyClassInstance.PropA}"
Also, the grid is set to allow the user to add rows. What I forgot to do on a row add was arrange for MyClassInstance to be initialized, therefore it was null. And when I removed focus from the new row, I would get the Two-way binding requires Path or XPath exception.
I found the real cause when in my attempts to fix one of the methods caused me to get the null value exception.
The stack trace for the binding needs a path error was quite extensive, which tells me that the developer once they got to the deep into the calls, guessed that the most likely reason for the null value was the path problem. Personally, for me it would have been more useful for the message of the exception to say "null value encountered, check that a path or xpath was specified."
当你在一天结束时感到疲倦时,有时会做一件奇怪的小事。
我有一个 Datagrid 绑定到一个名为 Dependent 的实体框架实体 - 字段 Name_of_Dependant/Relationship/Date_of_Birth。
不知何故,我决定创建新属性:全名/关系/出生日期 - 所有正确的类型。
因此,在我的数据网格中,我绑定到
但是我的列应该绑定到我的实体表依赖项中的列。
所以这是正确的:
这是错误的:
One can sometimes do a weird little thing when you get tired at the end of the day.
I have a Datagrid binding to an entity Framework Entity called Dependant - fields Name_of_Dependant / Relationship / Date_of_Birth.
Somehow I decied to create new properties : FullNames / Relationship / DateOfBirth - all the right types.
So in my datagrid I bind to an
But my columns should then bind to the columns in my entity table Dependant.
So this is correct:
This was wrong: