如何修复“双向绑定需要 Path 或 XPath"?WPF Datagrids 中的异常? [英] How do I fix "Two-way binding requires Path or XPath" exception in WPF Datagrids?

查看:19
本文介绍了如何修复“双向绑定需要 Path 或 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 时,当我尝试编辑某个值(整数) 列,尽管编辑字符串列一直工作正常.我不明白为什么我会得到这个,或者我该怎么做.

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:

{ 得到;放;}

如:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆