本文介绍了如何修复“双向绑定需要路径或XPath” WPF Datagrids中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

此问题的背景/上下文:
我有一个WPF桌面应用程序。它使用LINQ to SQL连接到其SQL数据库,并在WPF Datagrids中显示其数据。它工作得很好,但性能是一个问题,因为LINQ可能是致命的慢,所以我已经把我的逻辑和UI控件从LINQ数据库上下文切换到尽可能多的位置,而是将它们加载到非常类似于LINQ对象,大大提高了性能。

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.

问题:
当我测试我的Datagrids,我现在得到一个新的例外 双向绑定需要Path或XPath 当我尝试编辑一个Datagrid的某个(整数)列的值时,尽管编辑字符串列已被工作正常我不明白为什么我得到这个,或者做什么。

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.

所以当datagrid.datacontext设置为LINQ实体关联时,它工作,但是当它设置为普通对象的列表时,它几乎可以工作。我尝试将列表更改为一个ObservableCollection,但这并没有明显的效果。

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.

目前我有:

<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小时的延迟。

I found a very simple fix shortly after typing this, which I'll post when the site lets me after its 8-hour delay.

推荐答案

好的,打了所有的东西,我尝试了一些有用的东西。我正在发布,以防其他人帮助。

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组;

如下:

    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; }

这篇关于如何修复“双向绑定需要路径或XPath” WPF Datagrids中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:32