简体   繁体   中英

How to get value of selected item from multicolumn ComboBox in WPF

    <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();

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 fe) and add your text blocks (named) to it in order and then

     string txt = myCollectionOfTextBlocks[ASelect.SelectedIndex];

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM