尝试使用Text
这样的属性:
XAML
<ComboBox Name="MyComboBox" IsEditable="True" IsTextSearchEnabled="True" SelectedIndex="0" Width="150" Height="30">
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>6</ComboBoxItem>
</ComboBox>
<Button Width="100" Height="30" Content="GetEditedText" VerticalAlignment="Bottom" Click="Button_Click" />
Code behind
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(MyComboBox.Text.ToString());
}
或通过模板访问TextBox
:
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBox input = ((TextBox)MyComboBox.Template.FindName("PART_EditableTextBox", MyComboBox));
MessageBox.Show(input.Text.ToString());
}