编辑 wpf 数据网格时“双向绑定需要 Path 或 XPath”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24258860/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:56:13  来源:igfitidea点击:

"Two-way binding requires Path or XPath" when edit wpf datagrid

c#wpfxamlxpathwpfdatagrid

提问by Boyi Li

ContractListUserControl.XAML

合同列表用户控件.XAML

<DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding Path=ContractList}"
              SelectedItem="{Binding Path=SelectedContract}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Person.LastName}" Header="Last Name" />
            <DataGridTextColumn Binding="{Binding Path=Person.GivenName}" Header="Given Name" />
            <DataGridTextColumn Binding="{Binding Path=ContractStart, StringFormat=dd/MM/yyyy, Mode=TwoWay}" Header="Contract Start" />
            <DataGridTextColumn Binding="{Binding Path=ContractEnd, StringFormat=dd/MM/yyyy, Mode=TwoWay}" Header="Contract End" />
        </DataGrid.Columns>
</DataGrid>

Contract.cs

合同.cs

public class Contract
{
    public DateTime ContractStart { get; set; }
    public DateTime ContractEnd { get; set; }
    public Person Person { get; set; }
}

Person.cs

人物.cs

public class Person
{
    public string LastName { get; set; }
    public string GivenName { get; set; }
}

ViewModel.cs

视图模型.cs

public class ContractListViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Contract> _contractList;
    public ObservableCollection<Contract> ContractList
    {
        get { return _contractList; }
        set { SetField(ref _contractList, value, () => ContractList); } // Same as OnPropertyChanged
    }

    private Contract _selectedContract;
    public Contract SelectedContract
    {
        get { return _selectedCrew; }
        set { SetField(ref _selectedCrew, value, () => SelectedCrew); }
    }
}

If I set the datagrid as readonly, it works fine, problem is when I edit the LastName and GivenName DataGrid Column directly, it will crash, and throw the InvalidOperationException with message "Two-way binding requires Path or XPath". But if I just edit the ContractStart and ContractEnd it works fine.

如果我将数据网格设置为只读,它工作正常,问题是当我直接编辑 LastName 和 GivenName DataGrid 列时,它会崩溃,并抛出 InvalidOperationException 并带有消息“双向绑定需要路径或 XPath”。但是如果我只是编辑 ContractStart 和 ContractEnd 它工作正常。

I searched for some help, and I think I meet the same situation with this guy: DataGrid - "Two-way binding requires Path or XPath."

我搜索了一些帮助,我想我遇到了与这个人相同的情况: DataGrid - “双向绑定需要 Path 或 XPath。”

So the problem is that the Person Property is null, and the answer said that I should initialize the object that binding in the DataContext but didn't say how to do that.

所以问题是 Person 属性为空,答案说我应该初始化绑定在 DataContext 中的对象,但没有说明如何做到这一点。

回答by pushpraj

to achieve the initialization of Person property you may modify as follows

要实现 Person 属性的初始化,您可以修改如下

public class Contract
{
    public Contract()
    {
        Person = new Person();
    }

    public string RankName { get; set; }
    public string RankShortName { get; set; }
    public Person Person { get; set; }
}

add a constructor and initialize accordingly

添加一个构造函数并相应地初始化