[英]WPF Combobox get selected item text and associate an internal value (not displayed) to each item of the combobox
I have below WPF combobox: 我有以下WPF组合框:
<ComboBox x:Name="MyComboBox"
Grid.Column="1"
SelectionChanged="MyComboBox_SelectionChanged">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBoxItem Name="cbiEmployeeManagerType">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/Manager.png" />
<TextBlock Foreground="AliceBlue"
VerticalAlignment="Center">Manager</TextBlock>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Name="cbiEmployeeEngineerType">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/Engineer.png" />
<TextBlock Foreground="AliceBlue"
VerticalAlignment="Center">Engineer</TextBlock>
</StackPanel>
</ComboBoxItem>
</ComboBox>
First problem : 第一个问题 :
Within MyComboBox_SelectionChanged
I know how to detect which item is currently selected in the combobox through MyComboBox.SelectedIndex
but I do not know how to get the text that appears displayed and currently selected in the combobox. 在MyComboBox_SelectionChanged
我知道如何通过MyComboBox.SelectedIndex
检测组合框中当前选择的项目,但我不知道如何获取显示的文本以及在组合框中当前选择的文本。 For example, If I select the second item in my combobox, I want to obtain "Engineer". 例如,如果我在组合框中选择第二个项目,则想获得“工程师”。 How can I do it? 我该怎么做?
Second problem : 第二个问题 :
Also I would like to do the same as in combobox winforms in which you can display a member ( DisplayMember
property of combobox in winforms) and internally to associate it a member value ( ValueMember
property of combobox in winforms) which you can read when you selected the item within the combobox. 我也想做与组合框winforms相同的操作,在其中您可以显示一个成员(winforms中combobox的DisplayMember
属性),并在内部将其关联到一个成员值(winforms中combobox的ValueMember
属性),您可以在选择时读取该成员值组合框中的项目。 For example suppose the following combox items with their associated values. 例如,假设以下combox项及其关联值。
So "Manager" and "Engineer" would be displayed in the combox and when I would select "Manager" I would obtain its associated value, that is, 1000A, the same for Engineer. 因此,“经理”和“工程师”将显示在combox中,当我选择“经理”时,我将获得其关联的值,即1000A,与工程师相同。 Is it possible in WPF? 在WPF中可以吗? If so how? 如果可以,怎么办? I have read that it is possible using DisplayMemberPath
and SelectedValuePath
combobox properties but I do not know how to do it. 我已经读过,可以使用DisplayMemberPath
和SelectedValuePath
组合框属性,但是我不知道该怎么做。 Do I need to create a class and populate the combo from there and then using binding? 我是否需要创建一个类并从那里填充组合,然后使用绑定? Any piece of code will be highly appreciated. 任何代码都将受到高度赞赏。
UPDATE : For second problem finally I have done similar to what is explained here and here . 更新 :对于第二个问题,我最后做的事情与这里和这里解释的类似。
For the first problem you can access to TextBlock of ComboBoxItem by digging through the SelectionChangedEventArgs e
of the SelectionChanged
event with this kind of line of code: 对于第一个问题,您可以通过以下代码行来挖掘SelectionChanged
事件的SelectionChangedEventArgs e
,从而访问ComboBoxItem的TextBlock:
private void MyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = e.AddedItems[0] as ComboBoxItem;
var itemStackPanel = selectedItem.Contents as StackPanel;
// Get the TextBlock object from 'itemStackPanel' object
// TextBlock is with index 1 because it is defined second
// after Image inside the StackPanel in your XAML
var textBlock = itemStackPanel.Children[1] as TextBlock;
// This variable will hold 'Engineer' or 'Manager'
var selectedText = textBlock.Text;
}
or you can use this short line that combines all the above code in one line: (the ?.
is C# 6 feature to check for null in case something goes wrong) 或者您可以使用以下短行将以上所有代码合并在一行中:( ?.
是C#6功能,以防万一出现问题)
var selectedText = (((e.AddedItems[0] as ComboBoxItem)?.Content as StackPanel)?.Children[1] as TextBlock)?.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.