0

我正在尝试学习如何将我的简单数据库(.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 中拖动了数据网格,但它们也没有填充。任何想法?

4

3 回答 3

1

如果它对其他人有帮助——我刚刚遇到了这个问题,在这种情况下,我尝试使用设计器(从 DataSet 中拖过一个表等)将绑定的 ListBox 添加到 Page 控件。

上面在我的主窗口上尝试它的提示有效,但经过进一步检查后,我相信我明白了原因。

这似乎是设计师的不足;在窗口上添加控件时,除了生成的 XAML 之外,它还会生成用于填充 Window_Loaded 中的表的代码块,例如

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    AdventureWorksLTDataSet = ((AdventureWorksProductsEditor.AdventureWorksLTDataSet)(this.FindResource("adventureWorksLTDataSet")));
    // Load data into the table Product. You can modify this code as needed.
    adventureWorksLTDataSetProductTableAdapter = new AdventureWorksProductsEditor.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter();
    adventureWorksLTDataSetProductTableAdapter.Fill(AdventureWorksLTDataSet.Product);
    productViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productViewSource")));
    productViewSource.View.MoveCurrentToFirst();
}

(参见http://msdn.microsoft.com/en-us/library/dd547149.aspx

但是,当控件被添加到单独的 Page 控件时,它并没有这样做,并且根据 OP 报告的内容,可能对于 UserControl 等也不会这样做。

因此,使用生成的代码填充表格的快速而肮脏的解决方法是在窗口上执行一次,然后从 Window_Loaded 中获取代码,然后撤消并将其插入 x_Loaded 以供您随后进入的另一个控件添加绑定控件。

于 2014-04-11T21:56:30.217 回答
0

好的,这个答案比我之前的答案好得多。在我的 Window.xaml 文件中,我需要向控件发送正确的上下文,如下所示:

      <my:UserControl1 HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="userControl11" VerticalAlignment="Top" DataContext="{Binding  Source={StaticResource accountViewSource}}" />

现在我的 UserControl 知道上下文,我从 UserControl.xaml 中删除其他上下文修改代码,并直接使用上下文:

<UserControl x:Class="test3.UserControl1"
         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="300" d:DesignWidth="300" xmlns:my="clr-namespace:test3" Loaded="UserControl_Loaded">
  <Grid >
    <ComboBox DisplayMemberPath="Name" Height="23" HorizontalAlignment="Left" ItemsSource="{Binding}" Name="accountComboBox" SelectedValuePath="ID" VerticalAlignment="Top" Width="120">
      <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
          <VirtualizingStackPanel />
        </ItemsPanelTemplate>
      </ComboBox.ItemsPanel>
    </ComboBox>
  </Grid>
</UserControl>

那应该这样做。

于 2010-05-01T22:57:03.653 回答
0

我将我的 .sdf 文件拉到一个全新的项目中,重新生成 DataSet,然后将 ComboBox 拖到窗口上。它工作正常!

然后我意识到这个项目与上一个项目之间的区别在于控件位于 UserControl 中。我添加了一个带有组合框的用户控件并进行了编译。UserControl 中的组合框为空,主窗口上的 ComboBox 已正确填充。

于 2010-05-01T22:03:30.803 回答