2

I defined a usercontrol:

<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>

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;
        }

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?

I set isHittestVisible to true

1 Answers1

2

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

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
  • 1
    I'd say "almost always" rather than "sometimes". If you don't draw something, then there's nothing there for WPF to hit test, and if an element's Background or Fill or whatever is null, then you're effectively asking not to paint in the background or fill. So the only situation in which a null background might *not* prevent hit-testability is if the element is ignoring the background property. – Ian Griffiths Nov 30 '10 at 10:17