“双向绑定需要 Path 或 XPath"编辑 wpf 数据网格时 [英] "Two-way binding requires Path or XPath" when edit wpf datagrid

查看:54
本文介绍了“双向绑定需要 Path 或 XPath"编辑 wpf 数据网格时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ContractListUserControl.XAML

ContractListUserControl.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

Contract.cs

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

Person.cs

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

ViewModel.cs

ViewModel.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); }
    }
}

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

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.

我寻求了一些帮助,我想我遇到了这个家伙的相同情况:DataGrid - 双向绑定需要 Path 或 XPath."

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

所以问题是Person 属性为空,答案说我应该在DataContext 中初始化绑定的对象,但没有说怎么做.

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属性的初始化可以修改如下

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

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

这篇关于“双向绑定需要 Path 或 XPath"编辑 wpf 数据网格时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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