Display name from foreign Key constraint within a combobox

喜夏-厌秋 提交于 2019-12-23 21:15:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!