无法通过两种方式绑定对象内部的对象

时间:2019-05-12 18:15:38

标签: c# wpf binding datagrid

我试图以两种方式在一个数据网格中绑定一个类。

类的结构如下:

class Company : BaseProps
{
    public string CompanyName { get; set; }

    public Employee Employee { get; set; }

    public List<WorkTime> WorkTimes { get; set; }
}

员工看起来像这样:

class Technique : BaseProps
{
    public string Name { get; set; }

    public double Value { get; set; }
}

现在我正在尝试将其绑定到我的数据网格

<DataGrid Name="CompanyGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" Height="Auto" Margin="10,10,0,33.6" Width="Auto" Grid.RowSpan="2">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding CompanyName}" Header="Kompānijas nosaukums"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Employee.Name}" Header="Vadītāja vārds"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Employee.Surname}" Header="Vadītāja uzvārds"></DataGridTextColumn>
        </DataGrid.Columns>
</DataGrid>

当我输入CompanyName时,什么也没有发生。当我尝试输入Employee.Name或Surname时,出现以下错误:

System.InvalidOperationException:“双向绑定需要Path或XPath。”

1 个答案:

答案 0 :(得分:-1)

好像添加Mode = TwoWay可以修复错误...