[英]Getting Text from ComboBox in WPF
我在WPF中有一個ComboBox,但無法訪問其選定的項目文本。
我努力了
cbItem.Text;
cbItem.SelectedItem.ToString();
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();
嘗試
cbItem.SelectedValue.ToString()
僅當組合框值與組合框文本相同時,此方法才有效
編輯:
解決方案1
您必須訪問ComboBox的TextBox:
var str = (TextBox)cbItem.Template.FindName("PART_EditableTextBox", cbItem);
然后,您可以訪問該TextBox的SelectedText屬性:
var selectedText = str.SelectedText; // This will give you text of selected item
解決方案2
ComboBoxItem typeItem = (ComboBoxItem)cbItem.SelectedItem;
string value = typeItem.Content.ToString();// This will give you text of selected item
嘗試這個
<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.