[英]c# WPF bind combobox to TPH in Entity Framework code first using LINQ
我首先使用數據庫的實體框架代碼構建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>()
,則它會正確顯示值,但不會考慮區分.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"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.