我有两个实体:(实体框架代码第一种方法)
public class Work
{
public Guid Id { get; set; }
public string Title { get; set; }
public virtual Department Department { get; set; }
public virtual Guid DepartmentId { get; set; }
}
public class Department
{
public Department()
{
Works = new ObservableCollection<Work>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Work> Works { get; private set; }
}
我有以下XAML :( DataGrid工作项)
(我想显示一个DataGridComboBoxColumn
,其中包含所有部门名称)
<DataGrid.Columns>
<DataGridTextColumn x:Name="idColumn5" Binding="{Binding Id}" Header="Id" Width="100"/>
<DataGridTextColumn x:Name="titleColumn2" Binding="{Binding Title}" Header="Title" Width="250"/>
<DataGridComboBoxColumn SelectedValueBinding="{Binding Path=Department.Name, Mode=TwoWay,
NotifyOnTargetUpdated=True}" Header="Department Name"
ItemsSource="{x:Static my:MainWindow.Departments}"
DisplayMemberPath="Name" SelectedValuePath="ID"
Width="140"/>
<DataGridTextColumn x:Name="priceColumn2" Binding="{Binding Price}" Header="Price" Width="90"/>
</DataGrid.Columns>
VS 2012概述(错误)ItemsSource="{x:Static my:MainWindow.Departments}"
:
Invalid object name 'dbo.Departments'.
但我可以开始这个项目。
当我尝试添加新作品时(内部内部异常):
INSERT语句与FOREIGN KEY约束冲突 “FK_dbo.Works_dbo.Departments_DepartmentId”。冲突发生在 数据库“MyDbName”,表“dbo.Departments”,列'id'。
此外,我在MainWindow对象中有此属性:
public static ICollection<Department> Departments
{
get
{
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
return new List<Department>();
return _context.Departments.ToList();
}
}
我是WPF的新手,也是这种类型的数据绑定和XAML 将会提供任何有助于解决问题的答案。
答案 0 :(得分:0)
只需将Binding Path=
从Department.Name
更改为DepartmentId
)