[英]c# WPF bind combobox to TPH in Entity Framework code first using LINQ
I'm building a WPF application using entity framework code first for the database. 我首先使用数据库的实体框架代码构建WPF应用程序。 I need to bind my combobox to a TPH table that has a discriminator for two types of entity.
我需要将组合框绑定到具有区分两种实体的TPH表。
I've tried to query like this in XAML.CS
: 我试图在
XAML.CS
这样查询:
var pet = (from Pet in db.Pet.OfType<Dog>()
select Pet).ToList();
ComboBoxDog.ItemsSource = pet;
And my combobox in XAML
还有我在
XAML
组合框
<ComboBox x:Name="ComboBoxDog" ItemsSource="{Binding Path = Name}" HorizontalAlignment="Left" Margin="203,81,0,0" VerticalAlignment="Top" Width="138" Height="28" SelectionChanged="cBoxServer_SelectionChanged"/>
If I remove Path=Name
from combobox
and .OfType<Server>()
from my query it's showing values correctly, but without taking in considerations the discriminator. 如果我从
combobox
删除Path=Name
,并从查询中删除.OfType<Server>()
,则它会正确显示值,但不会考虑区分.OfType<Server>()
。 I would like to show only Name attribute. 我只想显示Name属性。
You need to set the DisplayMemberPath ,DisplayMemberPath specifies the path to the display string property for each item. 您需要设置DisplayMemberPath,DisplayMemberPath为每个项目指定显示字符串属性的路径。 In your case
就你而言
<ComboBox x:Name="cBoxServer" ItemsSource="{Binding}" DisplayMemberPath="{Binding Name}" HorizontalAlignment="Left" Margin="203,81,0,0" VerticalAlignment="Top" Width="138" Height="28" SelectionChanged="cBoxServer_SelectionChanged"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.