在组合框中显示来自外部键约束的名称 [英] Display name from foreign Key constraint within a combobox

查看:210
本文介绍了在组合框中显示来自外部键约束的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表。 Employee,ClientEmp和Client。



ClientEmp拥有员工(EmployeeID)和客户(ClientID)的 ForeignKey



到目前为止,我能够在我的数据库中保存对ClientEmp的更改(使用 EF ), code> selected 客户端。但是我必须在文本框中手动输入EmployeeID ForeignKey 才能匹配数据以保存。



显然,如果用户不知道EmployeeID FK是如何输入到文本框 $ c>。



因此,是否有办法使用组合显示Employee Firstname的列表,然后保存到数据库?



我试过这个,但似乎不工作。 / p>

 < ComboBox Grid.Column =1Grid.Row =1Name =cbEmployeeName
ItemsSource ={Binding Source = {StaticResource Employee}}
DisplayMemberPath =FirstName
SelectedValuePath =EmployeeID>
< / ComboBox>

我也试过使用此链接此链接在这里帮助但没有效果。

解决方案



最后,我通过 WPF聊天室的帮助找到了帮助,并帮助从 Maverik



在我的xaml中,我定义了以下内容:

 < ResourceDictionary> 
< ObjectDataProvider x:Key =Employee
ObjectType ={x:Type vm:ClientBookKeepingViewModel}
MethodName =GetEmp>
< / ObjectDataProvider>
< / ResourceDictionary>

< ComboBox Grid.Column =1Grid.Row =1Name =cbEmployeeName
ItemsSource ={Binding Source = {StaticResource Employee}}
DisplayMemberPath =FirstName
SelectedValue ={Binding Path = EmployeeID}
SelectedValuePath =EmployeeID
/>

并在我的ViewModel中创建此方法以获取Employees列表;

  public IEnumerable< DataObjects.Employee> GetEmp()
{
return context.Employees;
}


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 :).

解决方案

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;
}

这篇关于在组合框中显示来自外部键约束的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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