问题
I have three tables. Employee, ClientEmp and Client.
ClientEmp has the ForeignKey of both Employee (EmployeeID) and Client(ClientID).
So far, I am able to save changes to ClientEmp in my database (using EF) based on the selected Client. But I have to type in the EmployeeID ForeignKey manually in a textbox to be able to match the data for it to save.
Obviously, this isn't very user friendly if a user has no idea what the EmployeeID FK is enter into the textbox either.
Therefore, is there a way to show a list of Employee Firstname's using a combo box from the Employee table and then save it to the database?
I have tried this, but it doesn't seem to work.
<ComboBox Grid.Column="1" Grid.Row="1" Name="cbEmployeeName"
ItemsSource="{Binding Source={StaticResource Employee}}"
DisplayMemberPath="FirstName"
SelectedValuePath="EmployeeID">
</ComboBox>
I have also tried to use This link and This link here to help but with no avail. What am I doing wrong?
Any help would be much appreciated :).
回答1:
In the end, I managed to figure it out with help from the WPF Chat room and help from Maverik.
Within my xaml I defined the following;
<ResourceDictionary>
<ObjectDataProvider x:Key="Employee"
ObjectType="{x:Type vm:ClientBookKeepingViewModel}"
MethodName="GetEmp">
</ObjectDataProvider>
</ResourceDictionary>
<ComboBox Grid.Column="1" Grid.Row="1" Name="cbEmployeeName"
ItemsSource="{Binding Source={StaticResource Employee}}"
DisplayMemberPath="FirstName"
SelectedValue="{Binding Path=EmployeeID}"
SelectedValuePath="EmployeeID"
/>
and within my ViewModel, created this method to get a list of Employees;
public IEnumerable<DataObjects.Employee> GetEmp()
{
return context.Employees;
}
来源:https://stackoverflow.com/questions/16416523/display-name-from-foreign-key-constraint-within-a-combobox