简体   繁体   English

如何从WPF中的多列ComboBox获取所选项的值

[英]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, 我尝试使用下面的代码,但它给stackpanel作为字符串,

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 你有一个属性ASelect.SelectedIndex,它指出了哪一个被选中,所以你可以创建一个列表收集你的TextBlocks(列表或词典fe)并按顺序添加你的文本块(命名)然后

     string txt = myCollectionOfTextBlocks[ASelect.SelectedIndex];

You need to dig deeper.... Go here... and use the FindChild method to find the TextBlock within your ComboBoxItem. 您需要深入挖掘.... 转到此处...并使用FindChild方法在ComboBoxItem中查找TextBlock。 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... 但是,如果您没有为控件命名,那么您可能需要对其进行一些更改,以便您可以搜索M级深度或其他任何内容的第N个子控件...

Once you have the child TextBlock you just use the .Text to get it. 一旦你有了孩子TextBlock,你只需使用.Text来获取它。

You are adding a complex type (StackPanel) as the items of your combobox. 您正在添加复杂类型(StackPanel)作为组合框的项目。 When you access the SelectedItem property of your combobox you are getting back the instance of the StackPanel object. 当您访问组合框的SelectedItem属性时,您将返回StackPanel对象的实例。

That is the extent that the combobox knows about it's items. 这就是组合框知道它的项目的程度。 It has no idea what is inside the StackPanel. 它不知道StackPanel里面是什么。

Like Myermian said you would need to crawl the visual tree in some way to figure out what you want. 就像Myermian所说,你需要以某种方式抓取视觉树来弄清楚你想要什么。

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. hacky方法是取回你返回的StackPanel实例并调用StackPanel.Children来获取它的孩子然后迭代它们并找到你想要的东西。 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. 你真正想要做的是数据绑定组合框并将UI与列表中的数据分开。 This way you can access the data you want (the text box value) regardless of the UI structure of the item 这样,无论项目的UI结构如何,您都可以访问所需的数据(文本框值)

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 欲了解更多信息,请访问此链接multi-column-combobox-in-c-wpf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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