在 XAML 中绑定 WPF ComboBox - 为什么它是空的?

发布于 2024-08-31 04:11:16 字数 1713 浏览 4 评论 0原文

我正在尝试学习如何将我的简单数据库(.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离旧人 2024-09-07 04:11:16

如果它对其他人有帮助 - 我刚刚遇到了这个问题,在这种情况下尝试使用设计器(从数据集中拖动表格等)将绑定的列表框添加到页面控件。

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

这似乎是设计师的缺陷;在窗口上添加控件时,除了生成的 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

没有因此,但是,当该控件被添加到单独的页面控件时,并且根据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.

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();
}

(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.

旧故 2024-09-07 04:11:16

我将 .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.

淡写薰衣草的香 2024-09-07 04:11:16

好的,这个答案比我之前的答案要好得多。在我的 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>

:应该这样做。

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:

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

Now that my UserControl knows the context, I remove other context-modifying code from the UserControl.xaml, and just use the context directly:

<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>

And that should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文