locked
How to bind a combobox? RRS feed

  • Question

  •  Hello!

    I have a combobox inside a stackpanel. I want my combobox to bind to the values i get from db.
    All other controls inside stackpanel bind perfectly, but the combo doesnt. Why?

    <ComboBox Style="{DynamicResource Template_DashboardComboBox}" Width="200" Height="18" IsSynchronizedWithCurrentItem="True" Panel.ZIndex="-2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,14,0,0" SelectedIndex="0" SelectedItem="{Binding Path=Culture}">
        
    <ComboBoxItem Style="{DynamicResource Template_WorkspaceQuickFindComboBoxComboBoxItemWithIcon}" Content="Portugues"/>
        
    <ComboBoxItem Style="{DynamicResource Template_WorkspaceQuickFindComboBoxComboBoxItemWithIcon}" Content="Espanol"/>
        
    <ComboBoxItem Style="{DynamicResource Template_WorkspaceQuickFindComboBoxComboBoxItemWithIcon}" Content="English"/>
        
    <ComboBoxItem Style="{DynamicResource Template_WorkspaceQuickFindComboBoxComboBoxItemWithIcon}" Content="Francais"/>
    </ComboBox>

    What should i do so my combobox works as i want? (and should)

    Thanks

    Sunday, November 23, 2008 9:25 PM

Answers

  • Here is an example with both a bound and unbound scenario:

    <Window x:Class="ComboBoxTest.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="300" Width="300" 
        x:Name="_window">  
        <Grid> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="*" /> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
                <RowDefinition Height="*" /> 
                <RowDefinition Height="*" /> 
            </Grid.RowDefinitions> 
     
            <UniformGrid Grid.Row="0" Columns="2">  
                <GroupBox Header="Unbound Items" Margin="5">  
                    <ComboBox x:Name="UnBoundCombo" Width="100" VerticalAlignment="Center" 
                              SelectedValuePath="Content"   
                              SelectedValue="{Binding ElementName=UnBoundText, Path=Text}">  
                        <ComboBoxItem Content="Item 1"/>  
                        <ComboBoxItem Content="Item 2"/>  
                        <ComboBoxItem Content="Item 3"/>  
                    </ComboBox> 
                </GroupBox> 
                <GroupBox Header="SelectedItem" Margin="5">  
                    <TextBox x:Name="UnBoundText" Text="Item x" VerticalAlignment="Center"/>  
                </GroupBox> 
            </UniformGrid> 
     
            <UniformGrid Grid.Row="1" Columns="2">  
                <GroupBox Header="Bound Items" Margin="5">  
                    <ComboBox x:Name="BoundCombo" Width="100" VerticalAlignment="Center" 
                              SelectedValue="{Binding ElementName=BoundText, Path=Text}"/>  
                </GroupBox> 
                <GroupBox Header="SelectedItem" Margin="5">  
                    <TextBox x:Name="BoundText" Text="Bound item x" VerticalAlignment="Center"/>  
                </GroupBox> 
            </UniformGrid> 
     
        </Grid> 
    </Window> 

    using System.Collections.Generic;  
    using System.Windows;  
     
    namespace ComboBoxTest  
    {  
        /// <summary>  
        /// Interaction logic for Window1.xaml  
        /// </summary>  
        public partial class Window1 : Window  
        {  
            public Window1()  
            {  
                InitializeComponent();  
     
                BoundCombo.ItemsSource = new List<string> {  
                    "Bound item 1",  
                    "Bound item 2",  
                    "Bound item 3",  
                };  
            }  
        }  
    }  
     

    hth,
    Marcel
    • Marked as answer by Marco Zhou Wednesday, November 26, 2008 5:04 AM
    Monday, November 24, 2008 12:20 PM

All replies

  • The binding in the ComboBox is thru the ItemSource property, it can be bound to any IEnumerable, for example an array of strings:

    string[] Cultures = new string[] { "Portugues", "Espanol" .... };

    <ComboBox ItemsSource="{Binding Path=Cultures}" ...
    • Proposed as answer by harriet_001 Sunday, December 11, 2011 4:08 PM
    • Unproposed as answer by harriet_001 Sunday, December 11, 2011 4:08 PM
    Sunday, November 23, 2008 10:35 PM
  • but i want to show the selected item as it is on the database
    imagine im showing an account. one of the items is the nationalty of the owner of the account. the combobox shows  it (even if it shows ALL nationalities avaiable)
     
    i understand i need 2 itemsource. one for the account (my case, stackpanel.datacontext) and other just for the combobox. but how to show what is stored on db?

    thanks
    Monday, November 24, 2008 12:42 AM
  • Hi,

    Bind the selected item to the value coming from the db.Think that should work.

    Or please give a small working code if possible

    Hope it helps

    FEAR NOT TO BE JUST
    Monday, November 24, 2008 4:04 AM
  • I think you should not use both SelectedIndex and SelectedItem at the same time. Not sure which one will win...

    hth,
    Marcel
    Monday, November 24, 2008 8:29 AM
  • Rahul P Nath said:

    Hi,

    Bind the selected item to the value coming from the db.Think that should work.

    Or please give a small working code if possible

    Hope it helps


    FEAR NOT TO BE JUST



    But its what im doing...
    SelectedItem="{Binding Path=Culture}">

    private void ListViewUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

                StackPanelTextBoxes.DataContext = BSEngine.AdminService.GetUser(ListViewUsers.SelectedItem.ToString());

    }
    This is what im doing on code behind,
    I just want to bind the value on the database to the combobox.
    If i take selectedindex off, it still doesnt show anything.
    Or if i didnt understand, how do i bind the selected item to the value coming from db?

    Thanks
    Monday, November 24, 2008 9:03 AM
  • Anyone?
    Monday, November 24, 2008 10:58 AM
  • Here is an example with both a bound and unbound scenario:

    <Window x:Class="ComboBoxTest.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="300" Width="300" 
        x:Name="_window">  
        <Grid> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="*" /> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
                <RowDefinition Height="*" /> 
                <RowDefinition Height="*" /> 
            </Grid.RowDefinitions> 
     
            <UniformGrid Grid.Row="0" Columns="2">  
                <GroupBox Header="Unbound Items" Margin="5">  
                    <ComboBox x:Name="UnBoundCombo" Width="100" VerticalAlignment="Center" 
                              SelectedValuePath="Content"   
                              SelectedValue="{Binding ElementName=UnBoundText, Path=Text}">  
                        <ComboBoxItem Content="Item 1"/>  
                        <ComboBoxItem Content="Item 2"/>  
                        <ComboBoxItem Content="Item 3"/>  
                    </ComboBox> 
                </GroupBox> 
                <GroupBox Header="SelectedItem" Margin="5">  
                    <TextBox x:Name="UnBoundText" Text="Item x" VerticalAlignment="Center"/>  
                </GroupBox> 
            </UniformGrid> 
     
            <UniformGrid Grid.Row="1" Columns="2">  
                <GroupBox Header="Bound Items" Margin="5">  
                    <ComboBox x:Name="BoundCombo" Width="100" VerticalAlignment="Center" 
                              SelectedValue="{Binding ElementName=BoundText, Path=Text}"/>  
                </GroupBox> 
                <GroupBox Header="SelectedItem" Margin="5">  
                    <TextBox x:Name="BoundText" Text="Bound item x" VerticalAlignment="Center"/>  
                </GroupBox> 
            </UniformGrid> 
     
        </Grid> 
    </Window> 

    using System.Collections.Generic;  
    using System.Windows;  
     
    namespace ComboBoxTest  
    {  
        /// <summary>  
        /// Interaction logic for Window1.xaml  
        /// </summary>  
        public partial class Window1 : Window  
        {  
            public Window1()  
            {  
                InitializeComponent();  
     
                BoundCombo.ItemsSource = new List<string> {  
                    "Bound item 1",  
                    "Bound item 2",  
                    "Bound item 3",  
                };  
            }  
        }  
    }  
     

    hth,
    Marcel
    • Marked as answer by Marco Zhou Wednesday, November 26, 2008 5:04 AM
    Monday, November 24, 2008 12:20 PM