如何修复 WPF Datagrids 中的“双向绑定需要路径或 XPath”异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9449689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fix "Two-way binding requires Path or XPath" exception in WPF Datagrids?
提问by Dronz
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.
这个问题的背景/上下文:我有一个 WPF 桌面应用程序。它使用 LINQ to SQL 连接到其 SQL 数据库,并在 WPF Datagrids 中显示其数据。它运行得相当好,但性能是一个问题,因为 LINQ 可能非常慢,所以我一直在尽可能地将我的逻辑和 UI 控件从 LINQ 数据库上下文中切换出来,而是将它们加载到非常相似的局部变量中LINQ 对象,这极大地提高了性能。
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.
问题:当我测试我的 Datagrids 时,我现在得到一个新的异常“双向绑定需要 Path 或 XPath。”当我尝试编辑一个 Datagrid 的某个(整数)列中的值时,尽管编辑字符串列有一直工作正常。我不明白为什么我会得到这个,或者该怎么做。
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.
因此,当 datagrid.datacontext 设置为 LINQ 实体关联时它起作用,但它几乎只在设置为纯对象列表时起作用。我尝试将列表更改为 ObservableCollection,但这没有明显效果。
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.
键入此内容后不久,我发现了一个非常简单的修复程序,当网站在 8 小时延迟后允许我时,我将发布该修复程序。
采纳答案by Dronz
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:
解决方案是将绑定的成员变量添加到问题中:
{ get; set; }
{ 得到; 放; }
As in:
如:
public System.Nullable<decimal> Protein { get; set; }
public System.Nullable<decimal> Carb { get; set; }
public System.Nullable<decimal> Fat { get; set; }
public System.Nullable<decimal> ProteinPercent { get; set; }
public System.Nullable<decimal> CarbPercent { get; set; }
public System.Nullable<decimal> FatPercent { get; set; }
public System.Nullable<int> NonPFWMeals { get; set; }
public System.Nullable<bool> Skip { get; set; }
回答by Lynn Crumbling
I ran into this problem with a bound textbox. My solution was to explicitly bind to ".":
我在绑定文本框时遇到了这个问题。我的解决方案是明确绑定到“.”:
<TextBox Text="{Binding Path=.}" />
That did the trick.
这就是诀窍。
回答by Mr Rubix
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.
“双向绑定需要 Path 或 XPath”错误可能是由于 XAML 中的路径名称与 c# Binding 元素中的路径名称稍有不同。大写字母真的很重要!那是我的问题。
XAML:
XAML:
<DataGridTextColumn Binding="{Binding Path=TotalHeure}" ClipboardContentBinding="{x:Null}" Header="Total Heures" Width="80"/>
C#:
C#:
public string Totalheure { get; set; }
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!
这将导致需要 Path 或 XPath 错误的双向绑定,因为它们不完全相同并且程序无法找到绑定路径!
Maybe it will help someone else who made the Same mistake
也许它会帮助犯同样错误的其他人
回答by Ryno Potgieter
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
当您在一天结束时感到疲倦时,有时会做一件奇怪的小事。我有一个 Datagrid 绑定到一个名为 Dependent 的实体框架实体 - 字段 Name_of_Dependant / Relationship / Date_of_Birth。不知何故,我决定创建新属性:FullNames / Relationship / DateOfBirth - 所有正确的类型。
所以在我的数据网格中,我绑定到一个
ObservableCollection<Dependant>
But my columns should then bind to the columns in my entity table Dependant. So this is correct:
但是我的列应该绑定到我的实体表 Dependant 中的列。所以这是正确的:
Binding="{Binding Name_of_Dependant}"
This was wrong:
这是错误的:
Binding="{Binding FullNames}"