views:

71

answers:

3
    <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();
+1  A: 

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];
lukas
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
if it is a static collection u can give them names :) if not xaml is xml so nodes, childrens and so on
lukas
Yes, it is a static collection but how do I get the text of the combobox item that has been selected?
developer
+1  A: 

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.

myermian
This is too confusing. Is'nt there a simpler way of getting the value?
developer
+1  A: 

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

Foovanadil