我正在尝试学习如何将我的简单数据库(.sdf)绑定到组合框。我创建了一个数据集,其中包含我的表。然后我将一个表从 DataSource 拖到我的控件上。没有构建警告/错误,当它运行时,ComboBox 是空的。
<UserControl x:Class="OurFamilyFinances.TabItems.TransactionTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="414" d:DesignWidth="578" xmlns:my="clr-namespace:OurFamilyFinances" Loaded="UserControl_Loaded_1">
<UserControl.Resources>
<my:FinancesDataDataSet x:Key="financesDataDataSet" />
<CollectionViewSource x:Key="accountViewSource" Source="{Binding Path=Account, Source={StaticResource financesDataDataSet}}" />
</UserControl.Resources>
<Grid>
<ComboBox DisplayMemberPath="Name" Height="23" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource accountViewSource}}" Margin="3,141,0,0" Name="accountComboBox" SelectedValuePath="ID" VerticalAlignment="Top" Width="120">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</Grid>
显示的路径是正确的,selectedPath 是“ID”,显示路径是“Name”。如果我在 Linq to Sql 中执行此操作,组合框会填充:
this.accountComboBox.ItemsSource = from o in db.Account
select new { o.ID, o.Name };
但我想学习如何在 XAML 中执行此操作。我也从 DataSource 中拖动了数据网格,但它们也没有填充。任何想法?