c#WPF首先使用LINQ将组合框绑定到实体框架代码中的TPH

本文关键字:框架 实体 代码 TPH 绑定 LINQ 组合 c#WPF | 更新日期: 2023-09-27 18:25:01

我首先使用数据库的实体框架代码构建一个WPF应用程序。我需要将我的组合框绑定到一个TPH表,该表具有两种类型实体的鉴别器。

我在XAML.CS:中尝试过这样的查询

 var pet = (from Pet in db.Pet.OfType<Dog>()
               select Pet).ToList();
 ComboBoxDog.ItemsSource = pet;

我在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"/>

如果我从combobox中删除Path=Name,从查询中删除.OfType<Server>(),那么它显示的值是正确的,但没有考虑鉴别器。我只想显示Name属性。

c#WPF首先使用LINQ将组合框绑定到实体框架代码中的TPH

您需要设置DisplayMemberPath,DisplayMemberPath为每个项指定显示字符串属性的路径。在您的情况下

<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"/>