UserControl不在HitTestResult中吗?

时间:2010-11-29 21:50:58

标签: c# .net wpf pixelsense

我定义了一个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>

然后我使用TagVisualization并执行自定义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,只有Codebox组成的UI元素(grid,border,surfacescrollviewer等)。但是我怎样才能获得Codebox本身?

我将isHittestVisible设置为true

1 个答案:

答案 0 :(得分:2)

尝试将用户控件的背景设置为Transparent。有时在WPF中,null({x:null},默认值)背景会阻止命中可测试性