WPF ComboBox在Ctrl + Z上撤消异常(关注SelectionChanged),为什么?

时间:2013-04-17 09:49:30

标签: wpf vb.net .net-4.0 combobox undo

嗯,有人可以告诉我为什么在这些情况下发生这种异常?

Test Window

<Window x:Class="MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="MainWindow" Height="350" Width="525">
  <StackPanel>
    <ComboBox
        Name="cmbTest"
        IsEditable="True"
        SelectionChanged="ComboBox_SelectionChanged">
    </ComboBox>
  </StackPanel>
</Window>

Code behind

Class MainWindow

  Sub New()
    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim source As New ObservableCollection(Of String)()
    source.Add("AAA")
    source.Add("BBB")
    source.Add("CCC")
    cmbTest.ItemsSource = source
  End Sub

  Private Sub ComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
    Keyboard.Focus(cmbTest)
  End Sub

End Class

现在,如果您键入AAA然后 Ctrl + Z ComboBox控件会引发此异常:

  

撤消单位打开时无法撤消或重做。

有人可以解释原因吗?!?怎么可能避免它?

1 个答案:

答案 0 :(得分:0)

&#34; Undo方法不适用于KeyPress或TextChanged事件。&#34; 这让我以前得到了。

来源:http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.undo.aspx