Why do my SelectedValue bindings don’t work in the ComboBox?

Platform: WPF| Category: ComboBox

One reason your binding to the SelectedValue may not work is if you have manually added ComboBoxItems as follows:

[XAML]
<ComboBox Name='mycombo' SelectedValue='{Binding Path=operator, Mode=TwoWay}' Width = '50'>   
	<ComboBoxItem Content=''></ComboBoxItem>  
	<ComboBoxItem>EQ</ComboBoxItem>  
	<ComboBoxItem>NE</ComboBoxItem>  
	<ComboBoxItem>CH</ComboBoxItem>  
	<ComboBoxItem>GE</ComboBoxItem>  
	<ComboBoxItem>LE</ComboBoxItem>  
</ComboBox> 

Instead, set the ItemsSource property to a list of strings as follows and the SelectedValue data binding will work fine:

[XAML]
<Window.Resources>  
        <src:MyList x:Name='myList' x:Key='myList'></src:MyList>  
    </Window.Resources>  
    
<ComboBox Name='mycombo' SelectedValue='{Binding Path=Code, Mode=TwoWay}'    
                  ItemsSource='{StaticResource myList}' Width = '50' Height='30' >  

Code Behind:

[C#]
    public class MyList : List   
    {   
        public MyList()    
        {   
            this.Add('');   
            this.Add('EQ');   
            this.Add('NE');   
            this.Add('CH');   
            this.Add('GE');   
            this.Add('LE');   
        }   
  
    }

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.