none
Is there an equivalent of value in a WPF ComboBoxItem?

    Question

  • I hope I'm just missing something obvious here, but I cannot find what I need anywhere.  What I want to do is manually populate a ComboBox with ComboBoxItems.  On those ComboBoxItems, I want to set a string for display and an integer that is the key for use behind the scenes.  I set the string to ComboBoxItem.Content, and it displays no problem.  But where do I set the integer?  I have tried every field I can think of, and none of them work, when I pick something from the drop down and then check the values in the debugger, the SelectedValue on the ComboBox is always null.  Every combo box I have ever used has supported this, and obviously, the WPF one does if it is databound with the SelectedValuePath property.  But how can I get this to work without databinding?

    Thursday, February 15, 2007 10:40 PM

All replies

  • Create a class which stores the string and it.  Set the ComboBoxItem.Content to an instance of that class.  Set the DisplayMemberPath to the string property name and the SelectedValuePath to the int property name.

    EDIT ----------------------

    Nevermind, I didn't notice that you don't want to databind until I read your post again.  My solution requires binding.

    Thursday, February 15, 2007 10:51 PM
  • AFAIK you can just treat a ComboBoxItem like a Windows Forms ComboBoxItem.  Simply set the Tag property to any object, and you can retrieve this at runtime and cast to your int.

    ComboBoxItem comboBoxItem = new ComboBoxItem();
    comboBoxItem.Content = "Five";
    comboBoxItem.Tag = 5;

    • Proposed as answer by Clare Sheehy Wednesday, July 04, 2012 2:49 PM
    Friday, February 16, 2007 12:28 AM
  •  

    could you please provide an example for this...

     item = new ComboBoxItem();
                    item.Name = "item"+dr["CampaignId"].ToString();
                    item.Content = dr["CampaignTitle"].ToString();
                    item.Tag = dr["CampaignTitle"].ToString();
                    cmb1.Items.Add(item);

     

      void cmb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                dock1.Visibility = Visibility.Visible;
                string query = "select * from  foo where id= "+cmb1.SelectedValue;
           }

    Tuesday, August 28, 2007 8:53 AM
  •  <ComboBox Height="23" Margin="317,14,0,0" Name="CboSearch" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" >

    <ComboBoxItem IsSelected="True" TextSearch.Text="SearchAll">(Anywhere)</ComboBoxItem>

    <ComboBoxItem TextSearch.Text="SearchName">By name</ComboBoxItem>

    <ComboBoxItem TextSearch.Text="SearchDescription">By description</ComboBoxItem>

    </ComboBox>

    Tuesday, December 09, 2008 2:56 AM
  • You don't have to use databinding to take advantage of SelectedValue. I believe the best way to acheive this would be the following.

    <ComboBox SelectedValuePath="Tag"
        <ComboBox.Items> 
            <ComboBoxItem Content="Display Text 1" Tag="Value1"/> 
            <ComboBoxItem Content="Display Text 2" Tag="Value2"/> 
         </ComboBox.Items> 
    </ComboBox> 

    Selected Value will then work because it will retrieve whatever is in Tag of the selected item.

    Cheers

    Justin

    http://jtango.wordpress.com
    • Proposed as answer by paqito Monday, October 11, 2010 8:31 PM
    Monday, December 15, 2008 5:15 AM
  • I know this is an old post, but i found it and it helped, and want to show a code only method i came up with

    ComboBox cmbo = new ComboBox();
    
    ComboBoxItem item = new ComboBoxItem();
    item.Content = "Default";
    cmbo.Items.Add(item);
    
    item = new ComboBoxItem();
    item.Content = "Alternate";
    cmbo.Items.Add(item);
    
    cmbo.SelectedIndex = 1;
    
    // this grabs the ComboBoxItem control that is selected
    Console.WriteLine(cmboWhere.SelectedItem);
    
    // convert it to something we can work with
    ComboBoxItem selected = (ComboBoxItem)cmbo.SelectedItem;
    Console.WriteLine(selected.Content);
    • Edited by kg6ypi Monday, June 14, 2010 3:17 AM yet another typo, last edit i swear!
    • Proposed as answer by Dude McMuffin Monday, February 28, 2011 7:38 PM
    • Unproposed as answer by Dude McMuffin Monday, February 28, 2011 7:39 PM
    Monday, June 14, 2010 3:09 AM
  • Many thanks to JTango. I was looking for a way to select a number of "working days" from a ComboBox, but giving the user a better description.

    So to give a complete example building on JTango's proposal, but including the data binding back to the view model:

    <ComboBox SelectedValuePath="Tag" SelectedValue="{Binding Path=WorkingDays}">
    	<ComboBoxItem Tag="1">1 day</ComboBoxItem>
    	<ComboBoxItem Tag="5">1 week</ComboBoxItem>
    	<ComboBoxItem Tag="10">2 weeks</ComboBoxItem>
    	<ComboBoxItem Tag="15">3 weeks</ComboBoxItem>
    </ComboBox>
    
    

    Thanks again.

    Thursday, April 14, 2011 9:42 AM
  • Thanks for this post!!!  I am new to WPF, MVVM, etc. and have tried every permutation imaginable over the past 24 hours to get my combobox populated by a CVS and bound to a simple field in my view model.  The Selected Value Path = "Tag" and adding the attribute to the CVS got me there!
    Tuesday, September 27, 2011 10:45 PM
  • When you put your own datas like selectbox to a combobox unless binding any datatable etc. you can use the way below;

    <ComboBox Width="160" x:Name="txtPublicationTypeID" SelectedValuePath="Tag">
       <ComboBox.Items>
          <ComboBoxItem Content="Item Text 1" Tag="1"/>
          <ComboBoxItem Content="Item Test 2" Tag="2"/>
       </ComboBox.Items>
    </ComboBox>

     

     

    Wednesday, October 19, 2011 1:09 PM