我首先使用数据库的实体框架代码构建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"/>
如果我删除Path=Name
的combobox
,并.OfType<Server>()
从我的查询它显示正确的价值观,但没有考虑在考虑鉴别。我只想显示Name属性。
您需要设置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"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句