Why click tree throws 'System.Windows.Documents.Run' is not a Visual or Visual3D' InvalidOperationException? : <p>Sometimes right-clicking treeviewitem results unhandled InvalidOperationException. In code behind I select the right clicked row:</p>

<pre><code>    static TreeViewItem VisualUpwardSearch(DependencyObject source)
    {
        while (source != null &amp;&amp; !(source is TreeViewItem))
            source = VisualTreeHelper.GetParent(source);

        return source as TreeViewItem;
    }

    private void OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
            TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);

            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;
            }
    }
</code></pre>

<p>According to stacktrace above is the source of the problem. </p>

<p>xaml:</p>

<pre><code>&lt;UserControl.Resources&gt;
   &lt;HierarchicalDataTemplate ItemsSource="{Binding ClassesItemsSource}" DataType="{x:Type pnls:FavoriteObjectTableViewModel}"&gt;
        &lt;StackPanel Orientation="Horizontal"&gt;
            &lt;Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="{Binding Bitmap}" /&gt;
            &lt;Label Content="{Binding TableName}"/&gt;
        &lt;/StackPanel&gt;
    &lt;/HierarchicalDataTemplate&gt;
    &lt;DataTemplate DataType="{x:Type pnls:FavoriteObjectClassViewModel}"&gt;
        &lt;StackPanel Orientation="Horizontal"&gt;
            &lt;Image Source="{Binding Bitmap, Converter={StaticResource UriToCachedImageConverter}}"/&gt;
            &lt;Label Content="{Binding ClassName}"/&gt;
        &lt;/StackPanel&gt;
    &lt;/DataTemplate&gt;
&lt;/UserControl.Resources&gt;

&lt;TreeView Name="Insert_ObjectTreeIE" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding TablesItemsSource}"&gt;
        &lt;TreeView.ItemContainerStyle&gt;
            &lt;Style TargetType="TreeViewItem"&gt;
                &lt;Setter Property="IsSelected" Value="{Binding IsSelected}" /&gt;
                &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
                &lt;EventSetter Event="PreviewMouseRightButtonDown" Handler="OnPreviewMouseRightButtonDown"&gt;&lt;/EventSetter&gt;
                &lt;EventSetter Event="MouseDoubleClick" Handler="OnMouseDoubleClick" /&gt;
            &lt;/Style&gt;
        &lt;/TreeView.ItemContainerStyle&gt;
&lt;/TreeView&gt;
</code></pre>

<p>Stacktrace:</p>

<pre><code>e.StackTrace    "   at MS.Internal.Media.VisualTreeUtils.AsVisual(DependencyObject element, Visual&amp; visual, Visual3D&amp; visual3D)\r\n   
at MS.Internal.Media.VisualTreeUtils.AsNonNullVisual(DependencyObject element, Visual&amp; visual, Visual3D&amp; visual3D)\r\n
at System.Windows.Media.VisualTreeHelper.GetParent(DependencyObject reference)\r\n   
at Tekla.Nis.Application.Shared.UI.Panels.FavoriteObjectsView.VisualUpwardSearch(DependencyObject source) in c:\\XXX\\161wpf\\src\\SharedAppFeature\\Panels\\FavoriteObjectsView.xaml.cs:line 45\r\n   
at Application.Shared.UI.Panels.FavoriteObjectsView.OnPreviewMouseRightButtonDown(Object sender, MouseButtonEventArgs e) in c:\\XXX\\161wpf\\src\\NisSharedAppFeature\\Panels\\FavoriteObjectsView.xaml.cs:line 52\r\n   
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)\r\n   
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)\r\n   
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)\r\n   
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)\r\n   
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)\r\n   
at System.Windows.UIElement.OnPreviewMouseDownThunk(Object sender, MouseButtonEventArgs e)\r\n   
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)\r\n   
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)\r\n   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)\r\n   
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)\r\n  
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)\r\n   at System.Windows.ContentElement.RaiseTrustedEvent(RoutedEventArgs args)\r\n   
at System.Windows.Input.InputManager.ProcessStagingArea()\r\n   
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)\r\n   
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)\r\n   
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)\r\n   
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)\r\n   
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)\r\n   
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)\r\n   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)\r\n   
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)\r\n   
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)\r\n   
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)\r\n   
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n   
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)\r\n   
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)\r\n  
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)\r\n   
at System.Windows.Application.RunDispatcher(Object ignore)\r\n   
at System.Windows.Application.RunInternal(Window window)\r\n   
at System.Windows.Application.Run(Window window)\r\n   
at System.Windows.Application.Run()\r\n   at "my application start location"
</code></pre>

<p>I can reproduce this only sometimes. My colleague said left click item 1 and right click item 2 produces this every time in certain tree.</p>
