0

Well, someone can tell me why this exception occurs in these conditions?

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

Now, if you type AAA and then Ctrl+Z, ComboBox control raise this exception:

Cannot Undo or Redo while undo unit is open.

Someone can explain why?!? How it would be possible to avoid it?

Jake1164
  • 12,291
  • 6
  • 47
  • 64
Luca Petrini
  • 1,695
  • 2
  • 28
  • 53
  • I have the same problem when moving focus in TextBox.TextChanged. If I put focus back manually (mouse/shiftTab) then Ctrl-Z crashes as well.. :s – EricG Apr 08 '15 at 13:36
  • For that particular problem I currently managed to bypass the problem when doin gthe Focus on the UIThread after I invoked it from a Timer.. >.< Just to see if it'd work. Working on more.. – EricG Apr 08 '15 at 14:53

1 Answers1

0

"The Undo method does not work with the KeyPress or TextChanged events." This has gotten me before.

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

MrDosu
  • 3,427
  • 15
  • 18