2013-07-17 50 views
0

在.net 3.5我有它得到這樣的組合框的當前編輯的文本實現:獲取可編輯的ComboBox的當前文本在.NET 4

dependencyObject.GetValue(ComboBox.TextProperty); 

一切正常和值的編輯的文本在ComboBox.Text-屬性。現在我們升級到.NET 4,返回值是舊文本,而不是編輯文本,這是第一個奇怪的行爲。但是,如果ComboBox的前一個值是ComboBox.ItemsSource中的項目,則上面的代碼將返回編輯後的值。目前我不知道微軟在.NET 4中改變了這個屬性。有沒有人有一個想法現在有什麼不同?

+2

winforms或WPF?標記它。 –

+0

其WPF應用程序 – freakmoder

+0

也就是說,您需要在ComboBox中使用當前文本? –

回答

1

嘗試使用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()); 
} 
相關問題