none
Binding on a combobox and setting default value

    Question

  • Hi

    Does anyone have any example code they can share with me demonstrating binding a combobox through MVVM and setting the default value (IsSelected) to one of the comboboxitems? I can do the bind but cannot seem to set the default item.

    Thanks

    Wednesday, June 23, 2010 5:56 AM

Answers

  • Hi,

    Expose a propeerty in your VM following is the code

    int _selectedvalue; 

    public int SelectedValue

    {

    get { return _selectedValue; }set { _selectedValue = value; RaisePropertyChanged("SelectedValue"); }

    }

     

    In your constructor set selectedvalue = 1;(Or your default value)

    In you XAML

    <ComboBox

    x:Name="cbo" Grid.Column="1" Grid.Row="4" ItemsSource="{Binding YourCollection}" DisplayMemberPath="Name" SelectedValuePath="CategoryId" SelectedValue="{Binding SelectedCategory, Mode=TwoWay}" HorizontalAlignment="Left" MaxWidth="250"/>

    That would work :-)

    HTH. Mark as answered if that helps

    Regards.

    Vidds

     

    Wednesday, June 23, 2010 6:36 AM
  • I have an example here: http://www.codeproject.com/KB/silverlight/SilverlightMVVMVideoPlay.aspx First, you MUST set the default value for SelectedIndex to -1. Then, I bind it like this: Then I simply change the value of SelectedVideoInListProperty to 0 to select the first item, or I search the SilverlightVideoList for the item I want and then set the SelectedVideoInListProperty to the index value of the item I want.
    Friday, June 25, 2010 5:45 PM

All replies

  • Wednesday, June 23, 2010 6:34 AM
  • Hi,

    Expose a propeerty in your VM following is the code

    int _selectedvalue; 

    public int SelectedValue

    {

    get { return _selectedValue; }set { _selectedValue = value; RaisePropertyChanged("SelectedValue"); }

    }

     

    In your constructor set selectedvalue = 1;(Or your default value)

    In you XAML

    <ComboBox

    x:Name="cbo" Grid.Column="1" Grid.Row="4" ItemsSource="{Binding YourCollection}" DisplayMemberPath="Name" SelectedValuePath="CategoryId" SelectedValue="{Binding SelectedCategory, Mode=TwoWay}" HorizontalAlignment="Left" MaxWidth="250"/>

    That would work :-)

    HTH. Mark as answered if that helps

    Regards.

    Vidds

     

    Wednesday, June 23, 2010 6:36 AM
  • I have an example here: http://www.codeproject.com/KB/silverlight/SilverlightMVVMVideoPlay.aspx First, you MUST set the default value for SelectedIndex to -1. Then, I bind it like this: Then I simply change the value of SelectedVideoInListProperty to 0 to select the first item, or I search the SilverlightVideoList for the item I want and then set the SelectedVideoInListProperty to the index value of the item I want.
    Friday, June 25, 2010 5:45 PM
  •  

    Binding Comobo:

    LoadOperation loadPhysican = ctxClaim837.Load(ctxClaim837.FillPhysicanComboQuery("", "", "", "", false, "Select s.LName + Case IsNull(s.FName, '') When '' Then '' Else ', ' + s.FName End + Case IsNull(s.MName, '') When '' Then '' Else ' ' + s.MName End Provider, s.StaffId From emrStaff s, emrGroups g, emrUsers u Where g.GroupId = s.GroupId And s.StaffId = u.StaffId And (u.AcIsActive = 1 Or s.StaffId = '" + StaffId_PhyCmb + "') And g.IsDoctor = 1 Order By Provider"), OnFillPhysicanCombo, null);

    private void OnFillPhysicanCombo(LoadOperation<ProviderDTO> operation)

    {

    if (operation != null)

    {

    if (operation.Entities.Count() > 0)

    {

    PhysicianComboBox = operation.Entities;

    }

    private void OnFillPhysicanCombo(LoadOperation<ProviderDTO> operation)

    {

    if (operation != null)

    {

    if (operation.Entities.Count() > 0)

    {

    PhysicianComboBox = operation.Entities;

    }

    SetPhysicianCmbIndex(StaffId_PhyCmb);

     

     

    }

     

     

    }

    Setting ComoboBox :

    SetPhysicianCmbIndex( PhysicianId)

    {

    var SelectedItems = PhysicianComboBox.Where(p => p.StaffId == PhysicianId).ToList(); if (SelectedItems!=null) { if (SelectedItems.Count>0) PhysicianSelectedIndex = PhysicianComboBox.ToList().IndexOf(SelectedItems[0]); }

    }

    Thanks,

    Ramesh

    Friday, July 02, 2010 9:48 AM
  • I am having a lot of trouble getting Vidds's suggestion to work: It seems he doesn't use the SelectedValue anywhere in the XAML. 


    Does anybody have a working project visualizing how you can load a combobox, bind it, and selecting a specific item in the combobox to be the default value?



    Thursday, July 29, 2010 10:24 AM
  • Hi Tricia,

    I have selectedvalue in the XAML

    <ComboBox  x:Name="cbo" Grid.Column="1" Grid.Row="4" ItemsSource="{Binding YourCollection}" DisplayMemberPath="Name" SelectedValuePath="CategoryId" SelectedValue="{Binding SelectedCategory, Mode=TwoWay}" HorizontalAlignment="Left" MaxWidth="250"/>

    What the issue you are facing. Let us know.


    HTH. Mark as answered if that helps :-)

    Regards,

    Vidds

     

     

    Friday, July 30, 2010 1:20 AM
  • Hi Vidds

    Thanks for the reply :) 

    I just assumed that the SelectedValue should be binded to the xaml, because in my case, setting the SelectedValue property to 1 in the constructor doesn't make much of a difference. But I am surely doing something wrong, but I really don't know what. 

    Here's my combobox (sorry for the long names): 


      <ComboBox x:Name="AllQuestionnaire"
                                      ItemsSource="{Binding GroupQuestionnaireCB, Mode=TwoWay}"
                                      SelectedValue="{Binding SelectedGroupQuestionnaireCB, Mode=TwoWay}"
                                      DisplayMemberPath="GroupQuestionnaireName"
                                      SelectedValuePath="GroupQuestionnaireID" />
                                      
                                      


    In my ViewModel I define the properties for GroupQuestionnaireCB and SelectedGroupQuestionaireCB. It is of tye GroupQUestionnaire, which I have defined in my Model  



       private ObservableCollection<GroupQuestionnaire> groupQuestionnaireCB;
            public ObservableCollection<GroupQuestionnaire> GroupQuestionnaireCB
            {
                get {
                     return groupQuestionnaireCB;  
                }
                set {
                    groupQuestionnaireCB = value;
                RaisePropertyChanged("GroupQuestionnaireCB");
                }
            }
    
            private int selectedGroupQuestionnaireCB;
            public int SelectedGroupQuestionnaireCB 
            {
                get {
                 
                   return selectedGroupQuestionnaireCB;
                }
                set { selectedGroupQuestionnaireCB = value;
            RaisePropertyChanged("SelectedGroupQuestionnaireCB");
               
                }
            }



    So, in my Constructor, I call the webservice and load the combobox, like this: 



       public MyConstructor()
            {
    int UID = 8; //example         
    
                WebService = new Service1Client();
           
                WebService.GetGroupQuestionnaireCompleted += new EventHandler<GetParentQuestionnaireCompletedEventArgs>(WebService_GetParentQuestionnaireCompleted);
                WebService.GetGroupQuestionnaireAsync(UID);
            }
        public void  WebService_GetGroupQuestionnaireCompleted(object sender, GetGroupQuestionnaireCompletedEventArgs e)
                {
                    GroupQuestionnaireCB = e.Result;
                 }



    So, I have defined the SelectedValue property as you proposed, and then I tried setting SelectedValue=1 both in the constructor, and in the method just above, where I populate the combobox. 

    The thing is, I can easily set the SelectedGroupQuestionnaire to whatever I want, but it doesn't change the fact, that although it has that value, the combobox isn't visually updated, ie. it still shows an empty item when loaded. 


    Does it make sense, that it doesn't work? 

    Thanks :) 


    Friday, July 30, 2010 7:44 AM
  • Oh, I found out what the problem was. 

    When you bind the combobox to the result of a WebService call, I have to set the selected value inside the Completed method... 

     public void  WebService_GetGroupQuestionnaireCompleted(object sender, GetGroupQuestionnaireCompletedEventArgs e)
                {
                    GroupQuestionnaireCB = e.Result;
                    SelectedGroupQuestionnaireCB = 1; //example
                 }

    So far I had SelectedGroupQuestionnaireCB after calling the Async method.. which of course doesn't make sense, because when you reach that it's very likely that the data has not yet been populated into the combobox... 




    Tuesday, August 03, 2010 5:36 AM