C# InkCanvas子级的命中测试
我通过代码隐藏的方式在InkCanvas中添加了一个按钮。因为我不明白的原因C# InkCanvas子级的命中测试,c#,wpf,inkcanvas,C#,Wpf,Inkcanvas,我通过代码隐藏的方式在InkCanvas中添加了一个按钮。因为我不明白的原因 HitTestResult result = VisualTreeHelper.HitTest(pe.InkCanvas.Children[2], point_MouseDown); 如果按钮的索引为2,则当我单击按钮时,结果始终为空 有人知道如何确定是否单击了子元素吗 任何帮助都将不胜感激。(是的,我在网上搜索没有成功。如果这是一个愚蠢的问题,我道歉) 系统:Windows 7、.net4.0 WPF C#
HitTestResult result = VisualTreeHelper.HitTest(pe.InkCanvas.Children[2], point_MouseDown);
如果按钮的索引为2,则当我单击按钮时,结果始终为空
有人知道如何确定是否单击了子元素吗
任何帮助都将不胜感激。(是的,我在网上搜索没有成功。如果这是一个愚蠢的问题,我道歉)
系统:Windows 7、.net4.0 WPF C#
编辑:如果我在Children[0]是RichTextBox的情况下执行相同的操作,则上面的HitTest将重新返回非null。有人知道为什么吗
编辑:RichTextBox和按钮都添加到代码隐藏中,XAML是:
<Grid Height="{x:Static local:pe.heightCanvas}" >
<!--NotepadCanvas. Canvas used to place user writing lines and borders.-->
<Canvas Name="NotePadCanvas" Panel.ZIndex="0"
Width="{x:Static local:pe.widthCanvas}"
Height="{x:Static local:pe.heightCanvas}"
Background="{Binding documenttype, Converter={StaticResource inkCanvasBackgroundConverter}}" />
<!--BackgroundCanvas. Canvas used for special highlighting of seleted items-->
<Canvas Name="BackgroundCanvas" Panel.ZIndex="1"
Width="{x:Static local:pe.widthCanvas}"
Height="{x:Static local:pe.heightCanvas}"
Background="Transparent" />
<!--FormsCanvas. Canvas used to place formatted text from lists, etc.-->
<Canvas Name="FormsCanvas" Panel.ZIndex="2"
Width="{x:Static local:pe.widthCanvas}"
Height="{x:Static local:pe.heightCanvas}"
Background="Transparent" />
<!--TranscriptionCanvas. Canvas used to place recognized ink from the InkAnalyzer-->
<Canvas Name="TranscriptionCanvas" Panel.ZIndex="3"
Width="{x:Static local:pe.widthCanvas}"
Height="{x:Static local:pe.heightCanvas}"
Background="Transparent" />
<!--InkCanv. Top most canvas used to gather handwritten ink strokes from the user. EditingMode="Ink" Gesture="OnGesture" -->
<local:CustomInkCanvas x:Name="InkCanvas" Panel.ZIndex="4"
Width="{x:Static local:pe.widthCanvas}"
Height ="{x:Static local:pe.heightCanvas}"
Background="Transparent"
AllowDrop="True" Drop="InkCanvas_Drop"/>
</Grid>
这是可行的,但它似乎很复杂,我不知道为什么按钮会显示为文本块:
...........
Button btn = new Button();
btn.Name = "Cancel";
btn.Content = "Cancel";
btn.Background = Brushes.Red;
btn.Width = 50;
btn.Height = 25;
btn.RenderTransform = new System.Windows.Media.TranslateTransform(pe.InkCanvas.ActualWidth-btn.Width,topmargin);
btnCancel = btn;
pe.InkCanvas.Children.Add(btnCancel);
然后,在PreviewMouseLeftButtonDown()中(代码在Microsoft网站上稍作修改)
// Clear the contents of the list used for hit test results.
hitResultsList.Clear();
// Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest(pe.InkCanvas, null,
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point_MouseDown));
for (int i = 0; i < hitResultsList.Count; i++)
{
TextBlock tb = hitResultsList[i] as TextBlock;
if (tb == null) continue;
if (tb.Text == "Cancel")
{
Cancel_Click();
}
}
List<DependencyObject> hitResultsList = new List<DependencyObject>();
// 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;
}
//清除用于命中测试结果的列表的内容。
hitResultsList.Clear();
//设置回调以接收命中测试结果枚举。
VisualTreeHelper.HitTest(pe.InkCanvas,null,
新的HitTestResultCallback(MyHitTestResult),新的PointHitTestParameters(point_MouseDown));
for(int i=0;i
// Clear the contents of the list used for hit test results.
hitResultsList.Clear();
// Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest(pe.InkCanvas, null,
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point_MouseDown));
for (int i = 0; i < hitResultsList.Count; i++)
{
TextBlock tb = hitResultsList[i] as TextBlock;
if (tb == null) continue;
if (tb.Text == "Cancel")
{
Cancel_Click();
}
}
List<DependencyObject> hitResultsList = new List<DependencyObject>();
// 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;
}
List hitsresultslist=new List();
//将命中测试的结果返回回叫。
公共HitTestResultBehavior MyHitTestResult(HitTestResult)
{
//将命中测试结果添加到枚举后要处理的列表中。
hitResultsList.Add(result.VisualHit);
//将行为设置为在所有z顺序级别返回视觉效果。
返回HitTestResultBehavior。是否继续;
}
有更好的办法吗?为什么按钮显示为文本块?您可以共享您的xaml树吗?因此,我们可以看一看,并尝试模拟。