0

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>
4

3 に答える 3

1

ITEM_ID物からITEM_NAME来ているのですか?

String textComboBox = ((ITEMCLASS)cbItem.SelectedItem).ITEM_NAME.ToString();
于 2013-09-11T13:33:54.247 に答える
0

試す

 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
于 2013-09-11T13:29:05.477 に答える