17

有时右键单击 treeviewitem 会导致未处理的 InvalidOperationException。在后面的代码中,我选择了右键单击的行:

    static TreeViewItem VisualUpwardSearch(DependencyObject source)
    {
        while (source != null && !(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;
            }
    }

根据上面的stacktrace是问题的根源。

xml:

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

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

堆栈跟踪:

e.StackTrace    "   at MS.Internal.Media.VisualTreeUtils.AsVisual(DependencyObject element, Visual& visual, Visual3D& visual3D)\r\n   
at MS.Internal.Media.VisualTreeUtils.AsNonNullVisual(DependencyObject element, Visual& visual, Visual3D& 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& handled)\r\n   
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)\r\n   
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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"

我只能有时重现这个。我的同事说左键单击项目 1 和右键单击项目 2 每次都会在某些树中产生这个。

4

2 回答 2

33

当您单击标签文本中的某个位置时,问题会重现。在这种情况下, thee.OriginalSource将是一个Run对象,它是Labelor的内部组合的一部分TextBox。该Run元素不是从Visual类继承的,因此不能成为可视化树的一部分,在这种情况下VisualTreeHelper.GetParent(source);会抛出InvalidOperationException

最简单的解决方案是制作每个文本控件(Label在您的情况下)IsHitTestVisible="False",这将从命中测试逻辑中排除这些控件,这意味着它永远不会是e.OriginalSource事件的,而是它的父级将被拾取并且很可能是父级将是一个Visual元素。

于 2016-07-12T10:03:03.833 回答
4

我发现与父母一起开始搜索对我来说是有效的。我的 TreeViewItem 文本由多次运行组成,因此我无法禁用命中测试。

    private void TextBlock_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        TreeViewItem treeViewItem;
        if (e.OriginalSource is System.Windows.Documents.Run)
            treeViewItem = VisualUpwardSearch(((System.Windows.Documents.Run)e.OriginalSource).Parent as DependencyObject);
        else treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);

        if (treeViewItem != null)
        {
            treeViewItem.IsSelected = true;
            e.Handled = true;
        }
    }
于 2019-09-30T15:16:38.293 回答