1
    <ComboBox Name="ASelect" Width="180" Height="27" SelectedIndex="0" HorizontalContentAlignment="Center" VerticalAlignment="Center" SelectionChanged="ASelect_SelectionChanged">
                 <ComboBoxItem HorizontalContentAlignment="Right" VerticalContentAlignment="Center">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                        <Image Source="a.png" Height="18" Width="22" />
                        <Label Content=" "/>
                    <TextBlock Width="150" Name="All"> All Values</TextBlock>
                </StackPanel>
                </ComboBoxItem>

                <ComboBoxItem HorizontalContentAlignment="Left">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="tick.png" Height="24" Width="24" />
                        <TextBlock Width="150"> New Values</TextBlock>
                    </StackPanel>
                </ComboBoxItem>

                <ComboBoxItem HorizontalContentAlignment="Left">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="question.png" Height="24" Width="24" />
                        <TextBlock Width="150"> Old Values</TextBlock>
                    </StackPanel>
                </ComboBoxItem>
</ComboBox>

How to get the value of selected item from a multicolumn combobox. I want to get the value in the textblock that says, "All values". I tried using the below the code, but it gives stackpanel as the string,

string selectionString = ((ComboBoxItem)ASelect.SelectedItem).Content.ToString();
developer
  • 5,178
  • 11
  • 47
  • 72
  • 1
    Even thought I gave a possible answer, I do wonder if those values are hard-coded (as you show in your example) or if they are related to some list which binds to the combobox. If you have a list, you can just get the SelectedIndex and use that to get the value you are looking for since you now have the index. – myermian Aug 04 '10 at 20:49

4 Answers4

1

give it a name

        <TextBlock Name="m_txtAllValues" Width="150"> All Values</TextBlock>

and then

         m_txtAllValues.Text = "yay it does work";

update: sorry i got u wrong :)

You have a property ASelect.SelectedIndex which indicated which one is selected so you could make a list Collection of your TextBlocks (List or Dictionary f.e) and add your text blocks (named) to it in order and then

     string txt = myCollectionOfTextBlocks[ASelect.SelectedIndex];
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108
  • Nope, my combobox has multiple ComboBox items. I have edited my question to show all the items that I have. I want to get the text of the selected item textblock only, how do I do that. Do I have to give name to textblocks in all the ComboBox items. If yes, then how do I get the text of the one that is selected? – developer Aug 04 '10 at 20:44
  • if it is a static collection u can give them names :) if not xaml is xml so nodes, childrens and so on – Lukasz Madon Aug 04 '10 at 20:52
  • Yes, it is a static collection but how do I get the text of the combobox item that has been selected? – developer Aug 04 '10 at 20:54
1

You need to dig deeper.... Go here... and use the FindChild method to find the TextBlock within your ComboBoxItem. Though, you might have to make some changes to it if you don't name your controls so that you can search for the Nth child control that is M levels deep or whatever...

Once you have the child TextBlock you just use the .Text to get it.

Community
  • 1
  • 1
myermian
  • 31,823
  • 24
  • 123
  • 215
1

You are adding a complex type (StackPanel) as the items of your combobox. When you access the SelectedItem property of your combobox you are getting back the instance of the StackPanel object.

That is the extent that the combobox knows about it's items. It has no idea what is inside the StackPanel.

Like Myermian said you would need to crawl the visual tree in some way to figure out what you want.

The hacky way is to take the StackPanel instance you get back and call StackPanel.Children to get it's children then iterate those and find what you want. However, that is a very fragile and generally not recommended approach.

What you really want to be doing is data binding the combo box and separating the UI from the data in the list. This way you can access the data you want (the text box value) regardless of the UI structure of the item

Brad Cunningham
  • 6,402
  • 1
  • 32
  • 39
1

please follow this code

string typeID="WHT01";
for (int i = 0; i < cmbWHTypeId.Items.Count; i++)
{
   EWareHouseTypes aWHType = (EWareHouseTypes)cmbWHTypeId.Items[i];
   if (aWHType.WhtID == typeID)
   {
      cmbWHTypeId.SelectedIndex = i;
      break;
   }
}

for more info visit this link multi-column-combobox-in-c-wpf

Stecya
  • 22,896
  • 10
  • 72
  • 102
Refat Sardar
  • 67
  • 2
  • 7