Binding WPF ComboBox in XAML - Why is it Empty?

Posted by mdiehl13 on Stack Overflow See other posts from Stack Overflow or by mdiehl13
Published on 2010-05-01T17:52:51Z Indexed on 2010/05/01 17:57 UTC
Read the original article Hit count: 447

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about wpf-binding

Related posts about wpf