gpt4 book ai didi

c# - UserControl 不在 HitTestResult 中吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:46 26 4
gpt4 key购买 nike

我定义了一个用户控件:

<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 元素(网格、边框、surfacescrollviewer 等)。但是我怎样才能得到 Codebox 本身呢?

我将 isHittestVisible 设置为 true

最佳答案

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

关于c# - UserControl 不在 HitTestResult 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com