locked
Set SelectedItem of one ComboBox to SelectedItem of another ComboBox RRS feed

  • Question

  • I am using SL 3 right now and I want to set one ComboBox.SelectedItem to another ComboBox.SelectedItem.

    Both ComboBoxs are bound to an Employee object and are on different tabs.

    I have both bindings set to TwoWay in XAML.

     In code I tried cboPersonTab2.SelectedItem = cboPersonTab1.SelectedItem. The SelectedItem for cboPerson2 remains null. 

     

    Wednesday, March 17, 2010 9:31 AM

Answers

  • Oh, I know where your problem is:

    SelecteItem can not be bind to a ID. 

    SelectedItem always means the selected Data Object in the list, not just a Field of the selected Data Object.

    If your main object (the one you are editing) has a EmployeeID field, it should also contains a Employee entity field. You need to bind to the Employee entity field, not the ID field.

     <ComboBox x:Name="cboPersonTab1" ItemsSource="{Binding Employee Mode=TwoWay}" DisplayMemberPath="Name" SelectedItem="{Binding YourEmployeeChildEntityField, Mode=TwoWay}" SelectionChanged="cboPersonTab1_SelectionChanged"></ComboBox>



    Wednesday, March 17, 2010 10:55 AM

All replies

  •  Do both ComboBoxes have the same ItemsSource?

     

     

    Wednesday, March 17, 2010 9:39 AM
  •  Do both ComboBoxes have the same ItemsSource?

     

     

     

    Yes

    Wednesday, March 17, 2010 9:44 AM
  • can you do one thing? Find out the hash code of the two selected items:

    cboPersonTab2.SelectedItem.GetHashCode()

    and check if they are same. If they are not same ( I feel, probably the Itemssource is cloned some where. I too struggled a lot with such things), means they are two different object, Then you need to find out the equivalent object and set that object as selecteditem.

    If they are same, this should work any way.

    Thanks,

    Radhey

    Wednesday, March 17, 2010 9:49 AM
  • can you do one thing? Find out the hash code of the two selected items:

    cboPersonTab2.SelectedItem.GetHashCode()

    and check if they are same. If they are not same ( I feel, probably the Itemssource is cloned some where. I too struggled a lot with such things), means they are two different object, Then you need to find out the equivalent object and set that object as selecteditem.

    If they are same, this should work any way.

    Thanks,

    Radhey

     

    Yes the hash codes are the same so I'm note sure why this doesn't work.

      

    <ComboBox x:Name="cboPersonTab1" ItemsSource="{Binding Employee, Mode=TwoWay}" DisplayMemberPath="Name" SelectionChanged="cboPersonTab1_SelectionChanged"></ComboBox>
    <ComboBox x:Name="cboPersonTab2" ItemsSource="{Binding Employee, Mode=TwoWay}" DisplayMemberPath="Name" SelectionChanged="cboPersonTab2_SelectionChanged"></ComboBox>
     

    Also tried binding to the SelectedItem

    <ComboBox x:Name="cboPersonTab1" ItemsSource="{Binding Employee, Mode=TwoWay}" DisplayMemberPath="Name" SelectedItem="{Binding ID}" SelectionChanged="cboPersonTab1_SelectionChanged"></ComboBox>
    <ComboBox x:Name="cboPersonTab2" ItemsSource="{Binding Employee, Mode=TwoWay}" DisplayMemberPath="Name" SelectedItem="{Binding ID}" SelectionChanged="cboPersonTab2_SelectionChanged"></ComboBox>
     
    Wednesday, March 17, 2010 10:09 AM
  • Ok sometimes this works and sometimes it doesn't. I didn't change anything related to this and it started working. I restarted the app without changing anything and now it doesn't work again. 

     

    UPDATE

     These ComboBoxes are on 2 different tabs. When I click on both tabs before I use the ComboBox then setting the SeletedItem of one ComboBox to the other works. I set a breakpoint on the constructors for the code behind on the ZAML page and the ViewModel and they don't get hit when changing tabs so I'm not sure what changing tabs does to make this work.

    Wednesday, March 17, 2010 10:29 AM
  • Oh, I know where your problem is:

    SelecteItem can not be bind to a ID. 

    SelectedItem always means the selected Data Object in the list, not just a Field of the selected Data Object.

    If your main object (the one you are editing) has a EmployeeID field, it should also contains a Employee entity field. You need to bind to the Employee entity field, not the ID field.

     <ComboBox x:Name="cboPersonTab1" ItemsSource="{Binding Employee Mode=TwoWay}" DisplayMemberPath="Name" SelectedItem="{Binding YourEmployeeChildEntityField, Mode=TwoWay}" SelectionChanged="cboPersonTab1_SelectionChanged"></ComboBox>



    Wednesday, March 17, 2010 10:55 AM
  • Well I decided to use the same ComboBox for both tabs so right now I don't need to have two of them. Thanks for your help. I may need this in the future.

    Wednesday, March 17, 2010 11:32 AM
  • What I said in my last post applies to any Combobox binding, so even you use the one Combobox, you still need to change the SelecteItem binding to the whole data item, not the ID.

    Wednesday, March 17, 2010 12:24 PM
  • This is what I need to understand but I don't
    What object are you refering to ?

    My ComboBox have ItemsSource set to a collection of MyObject that have ID and NAME

    what would be the SelectedItem binding mechanism to have it go 2 way on the Form.DataContext object ? which is the ID

    Thank you

     

    Monday, March 29, 2010 3:31 PM
  •  

    This is what I need to understand but I don't
    What object are you refering to ?

    My ComboBox have ItemsSource set to a collection of MyObject that have ID and NAME

    what would be the SelectedItem binding mechanism to have it go 2 way on the Form.DataContext object ? which is the ID

    Your ComboBox has ItemsSource set to a collection of MyObject that have ID and Name.

    Then the SelectedItem will be a MyObject. 

    Example:

    public class Company : Entity

    {

         ...

         public int CompanyTypeID{get;set;}

         public entity CompanyType{get;set;}

    }

    public class CompanyType:Entity

    {

        public int TypeID{get;set;}

       public string TypeName {get;set;}

    }

    Say you are trying to edit Company object.

    Company has a CompanyTypeID FK field you can assign by using a ComboBox. 

    You could use the following XAML to set the ComboBox binding which should be more straight forward:

    1) Use SelectedValue/SelectedValuePath:

    <ComboBox SelectedValuePath="TypeID", DisplayMemberPath="TypeName", SelectedValue="{Binding CompanyTypeID}" ItemsSource="{Binding ListOfCompanyTypes}" />

    SelectedValuePath:  CompanyType.TypeID, the Field that linked to FK field in the Main Object(Company).

    DisplayMemberPath: CompanyType.TypeName, the Field name you want to displayed in the ComboBox.

    SelectedValue: Bind to Company.CompanyTypeID Field.  This is the Field you really want to change by using this ComboBox.


    2) Or you could use SelectedItem Binding:

    Because Company also owns a CompanyType entity object, the ComboBox can also be used to select a CompanyType.  When you assign a selected CompanyType to the Company, The Company.CompanyTypeID field will get the ID from that selected CompanyType.

     <ComboBox  DisplayMemberPath="TypeName", SelectedItem="{Binding CompanyType}" ItemsSource="{Binding ListOfCompanyTypes}" />

     SelectedItem: Bind to Company.CompanyType field. Company.CompanyTypeID field will get filled automatically when you assign a CompanyType to the Company.

    I found method 1) has some problem with DataForm in Silverlight 4 beta and RC. DataForm won't sense any changes with ComboBox selection change. So the OK button won't be enabled to have you save the change. Or auto Commit won't happen if you use AutoCommit.  So for now, I always use the second method with ComboBox in DataForm.

     

     

     

     

     

    Monday, March 29, 2010 4:34 PM
  • sladapter

    Ok for the option 1, I don't know where you get the SelectedValuePath and the SelectedValue but they're not in Silverlight 3 or I'm missing something here. So option 1 doesn't work for me

    Hmm for option 2, me scenario is a bit different.

    I don't have a CompanyType in my Company object if I can refer to your example.
    Mine is a Foreign key scenario which Company just have the Foreign key of the selected object of the ComboBox.

    Lets get back to my thread which explain my scenario : http://forums.silverlight.net/forums/t/171636.aspx
    My last post is a very simple example of my problem.

    Tuesday, March 30, 2010 9:49 AM
  • I'm on Silverlight 4. If you could not find them in Silverlight 3, then they are probably new properties.

    Then you only have one option: SelectedItem.

     

     

    Tuesday, March 30, 2010 9:53 AM
  •  

    //------------------------------------------------------------------------------------------------------------------------------------------------
    // COMBO_TO_DATACONTEXT is the action to take foreign key from the SelectedItem of a ComboBox and set it to the SelectedOC property
    // DATACONTEXT_TO_COMBO is the action to take the value of the SelectedOC property and set the SelectedItem of the combo according to the property
    //------------------------------------------------------------------------------------------------------------------------------------------------
    
     
    Tuesday, March 30, 2010 10:09 AM
  • Yes, they are probably only on SL4.

    Then you only have one option: SelectedItem.

     

     

    Yes and here we are with the problem not working in my scenario which I have to do a method that takes the foreign key of the selected item of the combobox and save it myself to the DataContext and when I load the form it's the inverse, I do get the foreign key from the DataContext and search the foreign key in the collection of object from the combobox to finaly select the right one.

    Can't believe binding doesn't solve this problem

     

    Tuesday, March 30, 2010 10:13 AM
  • DataForm won't sense any changes with ComboBox selection change. So the OK button won't be enabled to have you save the change.
     

    If you still experience this issue: could you report the bug at http://silverlight.codeplex.com/workitem/list/basic ?

    Regards,

    Kasimier

    Tuesday, March 30, 2010 11:11 AM
  • If you still experience this issue: could you report the bug at http://silverlight.codeplex.com/workitem/list/basic ?

     

    I did report it first in the Silverlight Bug forum while in beta,

    http://forums.silverlight.net/forums/t/159679.aspx

    Then eichgraf told me to report the bug in the Connect site, so I did that as well.  But yesterday when I went to the Connect site to check the status of my report, I could not even find my original report.

    Now you tell me to report the bug in http://silverlight.codeplex.com/workitem/list/basic. Where is the site MS really track their bugs? If the bug forum is not monitored by the DEV team, why put it there in the first place?


     

     

     

    Tuesday, March 30, 2010 11:25 AM
  •  

    Now you tell me to report the bug in http://silverlight.codeplex.com/workitem/list/basic. Where is the site MS really track their bugs? If the bug forum is not monitored by the DEV team, why put it there in the first place?

    lol... I apologize, I wasn't aware that you posted the issue already. I wasn't even aware of the the bug forum until now :-) I'm still rather new to the silverlight thingies. Just wanted to ensure that this issue does not get forgotten.

    Regards,

    Kasimier

    Tuesday, March 30, 2010 11:44 AM
  •  No problem. I don't want the bug to be ignored either.

    Tuesday, March 30, 2010 11:54 AM
  • I want to set one ComboBox.SelectedItem to another ComboBox.SelectedItem.
     

    Just a note: I always find it easier to add a dedicated property to my ViewModel if I want to synchronize something in the UI. Your ComboBox(es) could then point to the same property. This property can automatically update your data model or whatever.

    Same for converters: I often see no reason for using Converters if I can simply add a property in my ViewModel(s) to provide the 'converted' value.

    Regards,

    Kasimier

    Tuesday, March 30, 2010 7:01 PM
  • Saturday, May 15, 2010 8:37 PM