locked
How to set the default value for a ComboBox in LightSwitch ? RRS feed

  • Question

  • Hi,

    I've created a ComboBox for my search screen and it works as expected.

    I'am using code for the binding:

     partial void SearchStatusTest_Created()
            {
                IContentItemProxy comboControl = this.FindControl("Status");

                comboControl.SetBinding(
                System.Windows.Controls.ComboBox.ItemsSourceProperty,
                "Screen.ManagerStatusFilter",
                System.Windows.Data.BindingMode.TwoWay);

                comboControl.SetBinding(
                System.Windows.Controls.ComboBox.SelectedItemProperty,
                "Screen.Status",
                System.Windows.Data.BindingMode.TwoWay);

               this.Status = this.DataWorkspace.ProjectDeck2Data.Statuses_SingleOrDefault(2);

            }


    Trying to set the default value using this.Status = this.DataWorkspace.ProjectDeck2Data.Statuses_SingleOrDefault(2):

    Notice that it does work, but still couldn't see the default value in the ComboBox on the screen.

    Any help really appreciated.

    Thanks.

    Bela
    Wednesday, November 27, 2013 3:23 PM

Answers

  • Hi Bela,

    >>reallyCoolcomboControl.Background = new SolidColorBrush(Color.FromArgb(51, 51, 51, 51));  //OK
         reallyCoolcomboControl.SelectedIndex = 0; //doesn't work

    I have no idea about why the first line is ok while the second isn't. As you described, there is no problem for a
    autocompletebox, it is suggested to use autocompletebox as a workaroud.

    In addition, you may refer to the question below for help:

    Filter data in combobox based on other combobox selected item



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by hbela Sunday, December 8, 2013 8:54 AM
    Thursday, December 5, 2013 1:49 AM

All replies

  • Hi Bela,

    You may refer to the method below in your Screen_Created method:

    var q = from m in this.DataWorkspace.YourData.COUNTRies
                 where m.country_name == this.PROFILE.country
                 select m;
     if (q.SingleOrDefault() != null)
     {
           this.prpCountry = q.SingleOrDefault();
     }

    You  may get detailed information from the similar question below:

    Lightswitch how to set the combobox value for a specific record from the database

    Hope it helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 29, 2013 3:33 AM
  • Hi Yunjie,

    I really appreciate your prompt reply.
    Just tried what is suggested, sorry to tell it doesn't work.
    For an autoCompletBox there is no problem:
    this.AutoCompleteBoxStatus = this.DataWorkspace.ProjectDeck2Data.Statuses_SingleOrDefault(2);

    Tried the following too for a combobox, see my comments pls.


    partial void SearchStatusTest_Activated()
            {
                IContentItemProxy comboControl = this.FindControl("Status");
               
       comboControl.ControlAvailable += comboControl_ControlAvailable;
          
               
            }
            void comboControl_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                     {
                         var reallyCoolcomboControl = e.Control as System.Windows.Controls.ComboBox;
                         
                         reallyCoolcomboControl.Background = new SolidColorBrush(Color.FromArgb(51, 51, 51, 51));  //OK

                         reallyCoolcomboControl.SelectedIndex = 0; //doesn't work

                         reallyCoolcomboControl.SelectedItem = this.DataWorkspace.ProjectDeck2Data.Statuses_SingleOrDefault(2); 

       //I can see in the local property Status (which is mapped to the Status comboBox ) the selected item, but
    it doesn't show up in a comboBox itself on the screen.

                     });

            }


    Thanks,

    Bela
    Saturday, November 30, 2013 3:59 PM
  • Hi Bela,

    >>reallyCoolcomboControl.Background = new SolidColorBrush(Color.FromArgb(51, 51, 51, 51));  //OK
         reallyCoolcomboControl.SelectedIndex = 0; //doesn't work

    I have no idea about why the first line is ok while the second isn't. As you described, there is no problem for a
    autocompletebox, it is suggested to use autocompletebox as a workaroud.

    In addition, you may refer to the question below for help:

    Filter data in combobox based on other combobox selected item



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by hbela Sunday, December 8, 2013 8:54 AM
    Thursday, December 5, 2013 1:49 AM