是一个用户控件不在HitTestResult?是一个、控件、用户、HitTestResult

2023-09-06 07:37:59 作者:迷途

我定义一个用户控件:

<s:SurfaceUserControl x:Class="Prototype_Concept_1.CodeBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="http://schemas.microsoft.com/surface/2008">
    <Grid>

            <Viewbox>
                <s:SurfaceScrollViewer Margin="10,10,10,10"
        x:Name="scroll"
        Width="250" 
        Height="250" 
        VerticalScrollBarVisibility="Visible" 
        HorizontalScrollBarVisibility="Visible"
        CanContentScroll="True">
                    <RichTextBox 
           Name="TextInput"
            AcceptsReturn="True"
                TextChanged="TextChangedEventHandler"
            Width="350"
            ScrollViewer.VerticalScrollBarVisibility="Hidden"
            ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                        <RichTextBox.Document>
                            <FlowDocument Name="flowDocument">
                            </FlowDocument>
                        </RichTextBox.Document>
                        <RichTextBox.Resources>
                            <Style TargetType="{x:Type Paragraph}">
                                <Setter Property="Margin" Value="0"/>
                            </Style>
                        </RichTextBox.Resources>
                    </RichTextBox>
                </s:SurfaceScrollViewer>
            </Viewbox>

    </Grid>
</s:SurfaceUserControl>

然后,我用一个TagVisualization,做一个自定义的HitTest:

Then i use a TagVisualization and do a custom Hittest:

private void TagVisualizer_VisualizationAdded(object sender, TagVisualizerEventArgs e)
        {

            Point pt = e.TagVisualization.Center;

            // Perform the hit test against a given portion of the visual object tree.
           hitResultsList.Clear();

            // Set up a callback to receive the hit test result enumeration.
            VisualTreeHelper.HitTest(MainGrid,
                              null,
                              new HitTestResultCallback(MyHitTestResult),
                              new PointHitTestParameters(pt));

            // Perform actions on the hit test results list.
            if (hitResultsList.Count > 0)
            {
                Console.WriteLine("Number of hits: " + hitResultsList.Count);
                foreach (DependencyObject o in hitResultsList)
                {

                    if (e.TagVisualization is LoupeTagVisualization)
                    {
                        if (o.GetType() == typeof(Ellipse))
                        {
                            Console.WriteLine(((o as Ellipse).Tag as SourceFile).getName());

                            CodeBox cb = new CodeBox();

                            MainScatter.Items.Add(cb);



                            break;
                        }
                    }
                    else if (e.TagVisualization is BinTagVisualization)
                    {
                        Console.WriteLine("BinTagVisualization");
                        Console.WriteLine(o.GetType());
                        if (o.GetType() == typeof(CheckBox))
                        {
                            (o as CheckBox).Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }


        }

        // Return the result of the hit test to the callback.
        public HitTestResultBehavior MyHitTestResult(HitTestResult result)
        {
            // Add the hit test result to the list that will be processed after the enumeration.
            hitResultsList.Add(result.VisualHit);

            // Set the behavior to return visuals at all z-order levels.
            return HitTestResultBehavior.Continue;
        }

现在的问题是,我真的不看codeBOX的结果,只有UI元素(网格,边框,surfacescrollviewer等)的codeBOX组成的。但我怎么能得到codeBOX本身?

The problem is, I don't actually see the Codebox in the results, only the UI elements (grid, border, surfacescrollviewer, etc) that the Codebox is composed of. But how can I get the Codebox itself?

我设置isHittestVisible为true

I set isHittestVisible to true

推荐答案

尝试在用户控件的背景设置为透明。有时在WPF中,空( {X:空} ,默认)的背景prevents命中测试性

Try setting the user control's background to Transparent. Sometimes in WPF, a null ({x:null}, the default) background prevents hit-testability