[英]Getting Text from ComboBox in WPF
I have a ComboBox in WPF and I cant access its selected item text. 我在WPF中有一个ComboBox,但无法访问其选定的项目文本。
I have tried 我努力了
cbItem.Text;
cbItem.SelectedItem.ToString();
XAML: XAML:
<ComboBox Name="cbItem" SelectedValuePath="ITEM_ID">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ITEM_NAME}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
ITEM_ID
和ITEM_NAME
是否来自某个对象?
String textComboBox = ((ITEMCLASS)cbItem.SelectedItem).ITEM_NAME.ToString();
Try 尝试
cbItem.SelectedValue.ToString()
This will work only if combobox values are same as the combobox text 仅当组合框值与组合框文本相同时,此方法才有效
EDIT: 编辑:
Solution 1 解决方案1
You have to get access to the ComboBox's TextBox: 您必须访问ComboBox的TextBox:
var str = (TextBox)cbItem.Template.FindName("PART_EditableTextBox", cbItem);
Then you can access the SelectedText property of that TextBox: 然后,您可以访问该TextBox的SelectedText属性:
var selectedText = str.SelectedText; // This will give you text of selected item
Solution 2 解决方案2
ComboBoxItem typeItem = (ComboBoxItem)cbItem.SelectedItem;
string value = typeItem.Content.ToString();// This will give you text of selected item
Try this 尝试这个
<ComboBox Name="cbItem" SelectedValuePath="ITEM_ID">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="txtblck" Text="{Binding ITEM_NAME}" />
</DataTemplate>
</ComboBox.ItemTemplate>
TextBox str = (TextBox)cbItem.FindName("txtblck");
string text = str.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.