locked
Data-bound value not appearing in ComboBox control (Windows Store app) RRS feed

  • Question

  • I have a ComboBox on a data entry form using the following XAML:

    <ComboBox x:Name="CityComboBox" Grid.Row="6" Grid.Column="1" Style="{StaticResource MyComboBoxStyle}" HorizontalAlignment="Stretch"
                                  DisplayMemberPath="DisplayValue" SelectedValue="{Binding City, Mode=TwoWay}" SelectedValuePath="City" 
                                  Foreground="Black"/>

    I am able to load the list items from a table but when I set the DataContext of the overall form, the data value of the City ComboBox is blank, and does not display the value of the underlying data. Other controls (i.e., TextBox controls) display their underlying data just fine. I've tried setting the CityComboBox.SelectedValue property to household.City (the underlying data), but it still doesn't display in the ComboBox. If I choose a value from the drop-down, it displays fine as the chosen value.

    It the syntax for the DisplaymemberPath, SelectedValue and SelectedValuePath properties correct? Am I missing a property, or are any of my properties mutually exclusive?

    <

    ComboBoxx:Name="CityComboBox"Grid.Row="6"Grid.Column="1"Style="{StaticResourceMyComboBoxStyle}"HorizontalAlignment="Stretch"


                                

    DisplayMemberPath="DisplayValue"SelectedValue="{BindingCity,Mode=TwoWay}"SelectedValuePath="City"

                                

    Foreground="Black"/>

    Thursday, March 28, 2013 8:23 PM

Answers

  • Here is working code - 

    <ComboBox Grid.Column="1" x:Name="CityComboBox" HorizontalAlignment="Center" ItemsSource="{Binding Items}"
                        Width="160" DisplayMemberPath="DisplayValue" SelectedValue="{Binding SelectedCity, Mode=TwoWay}" SelectedValuePath="City" 
                                  Foreground="Black"/>


     class DataModel
        {
            public String SelectedCity { get; set; }
    
            public List<ComboItem> Items
            {
                get
                {
                    List<ComboItem> items = new List<ComboItem>();
                    items.Add(new ComboItem("City1", "Display City One"));
                    items.Add(new ComboItem("City2", "Display City Two"));
                    items.Add(new ComboItem("City3", "Display CityThree"));
                    return items;
                }
            }
    
    
        }
        //DisplayMemberPath="DisplayValue" SelectedValue="{Binding City, Mode=TwoWay}" SelectedValuePath="City"
    
        class ComboItem
        {
            public ComboItem(string value, string displayValue)
            {
                DisplayValue = displayValue;
                City = value;
            }
    
            public string DisplayValue { get; set; }
            
    
            public String City { get; set; }
        }


      public MainPage()
            {
                this.InitializeComponent();
    
                DataModel viewModel = new DataModel();
                viewModel.SelectedCity = "City2";
                this.DataContext = viewModel;
                
            }

     


    Thanks, Sachin


    • Edited by Sachin S Thursday, March 28, 2013 11:33 PM
    • Proposed as answer by Jesse Jiang Monday, April 1, 2013 4:49 AM
    • Marked as answer by Jesse Jiang Wednesday, April 3, 2013 6:23 AM
    Thursday, March 28, 2013 11:33 PM

All replies

  • Can you try setting ItemsSource property for combo box to list of items or collection you are trying to populate the combobox? Or are you setting the datacontext in code behind? 

    Thanks, Sachin


    • Edited by Sachin S Thursday, March 28, 2013 8:32 PM
    Thursday, March 28, 2013 8:28 PM
  • Here is working code - 

    <ComboBox Grid.Column="1" x:Name="CityComboBox" HorizontalAlignment="Center" ItemsSource="{Binding Items}"
                        Width="160" DisplayMemberPath="DisplayValue" SelectedValue="{Binding SelectedCity, Mode=TwoWay}" SelectedValuePath="City" 
                                  Foreground="Black"/>


     class DataModel
        {
            public String SelectedCity { get; set; }
    
            public List<ComboItem> Items
            {
                get
                {
                    List<ComboItem> items = new List<ComboItem>();
                    items.Add(new ComboItem("City1", "Display City One"));
                    items.Add(new ComboItem("City2", "Display City Two"));
                    items.Add(new ComboItem("City3", "Display CityThree"));
                    return items;
                }
            }
    
    
        }
        //DisplayMemberPath="DisplayValue" SelectedValue="{Binding City, Mode=TwoWay}" SelectedValuePath="City"
    
        class ComboItem
        {
            public ComboItem(string value, string displayValue)
            {
                DisplayValue = displayValue;
                City = value;
            }
    
            public string DisplayValue { get; set; }
            
    
            public String City { get; set; }
        }


      public MainPage()
            {
                this.InitializeComponent();
    
                DataModel viewModel = new DataModel();
                viewModel.SelectedCity = "City2";
                this.DataContext = viewModel;
                
            }

     


    Thanks, Sachin


    • Edited by Sachin S Thursday, March 28, 2013 11:33 PM
    • Proposed as answer by Jesse Jiang Monday, April 1, 2013 4:49 AM
    • Marked as answer by Jesse Jiang Wednesday, April 3, 2013 6:23 AM
    Thursday, March 28, 2013 11:33 PM