在 XAML 中绑定 WPF ComboBox - 为什么它是空的?
我正在尝试学习如何将我的简单数据库(.sdf)绑定到组合框。我创建了一个数据集,其中包含我的表。然后,我将一个表从数据源拖到我的控件上。没有构建警告/错误,并且当它运行时,组合框是空的。
<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 中执行此操作。我也从数据源中拖动了数据网格,但它们也没有填充。有什么想法吗?
I am trying to learn how to bind my simple database (.sdf) to a combobox. I created a dataset with my tables in it. I then dragged a table from the DataSource onto my control. There are no build warnings/errors, and when it runs, the ComboBox is empty.
<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>
The paths that is shows are correct, the selectedPath is "ID" and the displaypath is "Name". If I do this in Linq to Sql, the combo box does populate:
this.accountComboBox.ItemsSource = from o in db.Account
select new { o.ID, o.Name };
But I would like to learn how to do this in XAML. I have dragged datagrids from the DataSource as well, but they are not populated either. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它对其他人有帮助 - 我刚刚遇到了这个问题,在这种情况下尝试使用设计器(从数据集中拖动表格等)将绑定的列表框添加到页面控件。
上面的提示在我的主窗口上尝试它是有效的,但经过进一步检查后,我相信我明白了原因。
这似乎是设计师的缺陷;在窗口上添加控件时,除了生成的 XAML 之外,它还会生成用于填充 Window_Loaded 中的表的代码块,例如
(请参阅 http://msdn.microsoft.com/en-us/library/dd547149.aspx)
它没有因此,但是,当该控件被添加到单独的页面控件时,并且根据OP报告的内容,对于用户控件等可能也不会这样做。
因此,利用生成的代码填充表的一个快速而肮脏的解决方法是在窗口上执行一次,然后从 Window_Loaded 中获取代码,然后撤消并将其插入到另一个控件的 x_Loaded 中,然后再将其插入到其他控件中。添加绑定控件。
In case it helps for others -- I just ran into this, in this case trying to use the designer (drag over a table from the DataSet, etc.) to add a bound ListBox to a Page control.
The tip above to try it instead on my main Window worked, but after a bit of further inspection I believe I see why.
It appears to be a deficiency with the designer; when adding the control on the window, in addition to the generated XAML, it does also generate the blob of code for populating the table within Window_Loaded, e.g.
(see http://msdn.microsoft.com/en-us/library/dd547149.aspx)
It didn't do so, however, when the control was added to a separate Page control, and presumably doesn't do so for a UserControl etc. either, based on what the OP reports.
So a quick and dirty workaround to make use of the generated code for populating the table is to just do it once on the window and then grab the code from Window_Loaded, then undo and plug it into the x_Loaded for the other control into which you then add the bound control.
我将 .sdf 文件拖入一个全新的项目中,重新生成数据集,并将 ComboBox 拖到窗口上。效果很好!
然后我意识到这个项目和上一个项目的区别在于控件位于 UserControl 中。我添加了一个带有组合框的用户控件并进行了编译。用户控件中的组合框是空的,主窗口上的组合框已正确填充。
I pulled my .sdf file into a completely new project, regenerated the DataSet, and dragged a ComboBox onto the window. It works fine!
Then I realized the difference between this project and the last was that the controls were in a UserControl. I added a usercontrol with a combobox and compiled. The combobox in a UserControl is empty, the ComboBox on the main window is filled correctly.
好的,这个答案比我之前的答案要好得多。在我的 Window.xaml 文件中,我需要将正确的上下文发送到控件,如下所示:
现在我的 UserControl 知道了上下文,我从 UserControl.xaml 中删除其他上下文修改代码,然后直接使用上下文
:应该这样做。
Okay, this answer is WAY better than my previous answer. In my Window.xaml file, I need to send the correct context to the control like this:
Now that my UserControl knows the context, I remove other context-modifying code from the UserControl.xaml, and just use the context directly:
And that should do it.