title: How can I cancel a user's WPF TreeView click?
body: <p>I've got a WPF application with a Treeview control.</p>
  
  <p>When the user clicks a node on the tree, other TextBox, ComboBox, etc. controls on the page are populated with appropriate values.</p>
  
  <p>The user can then make changes to those values and save his or her changes by clicking a Save button.</p>
  
  <p>However, if the user selects a different Treeview node without saving his or her changes, I want to display a warning and an opportunity to cancel that selection.</p>
  
  <p><a href="http://img522.imageshack.us/img522/2897/discardsj3.gif" rel="nofollow noreferrer">MessageBox: Continue and discard your unsaved changes?  OK/Cancel http://img522.imageshack.us/img522/2897/discardsj3.gif</a></p>
  
  <p>XAML...</p>
  
  <pre><code>&lt;TreeView Name="TreeViewThings"
      ...
      TreeViewItem.Unselected="TreeViewThings_Unselected"
      TreeViewItem.Selected="TreeViewThings_Selected" &gt;
  </code></pre>
  
  <p>Visual Basic...</p>
  
  <pre>
  Sub TreeViewThings_Unselected(ByVal sender As System.Object, _
                                ByVal e As System.Windows.RoutedEventArgs)
      Dim OldThing As Thing = DirectCast(e.OriginalSource.DataContext, Thing)
      If CancelDueToUnsavedChanges(OldThing) Then
          'put canceling code here
      End If
  End Sub
  
  Sub TreeViewThings_Selected(ByVal sender As System.Object, _
                              ByVal e As System.Windows.RoutedEventArgs)
      Dim NewThing As Thing = DirectCast(e.OriginalSource.DataContext, Thing)
      PopulateControlsFromThing(NewThing)
  End Sub
  </pre>
  
  <p>How can I cancel those unselect/select events?</p>
  
  <hr>
  
  <p>Update: I've asked a follow-up question...<br>
  <a href="https://stackoverflow.com/questions/543312/how-do-i-properly-handle-a-previewmousedown-event-with-a-messagebox-confirmation">How do I properly handle a PreviewMouseDown event with a MessageBox confirmation?</a></p>