问题描述
我只是解决了一个问题,尽管找到了解决方法,但我不理解。
摘要: ContentPresenter
的 ItemsControl
将获得预览点击事件,而不是包含该事件的实际屏幕。 为什么?
详细信息:
我的应用程序有一个包含各种UserControl的画布。单击事件在此画布上方的某个级别上进行处理-它的容器处理这些事件(在下面称为TestScreen
)。错误是当在画布中单击特定的控件类型时,该事件将不会得到处理。
有问题的UserControl在内部使用ItemsControl
来渲染一系列矩形。解决方法是在IsHitTestVisible="False"
上设置ItemsControl
。 (或者,这也适用于其ItemContainerStyle
。)
在Snoop中调试时,我注意到以下模式:
点击ItemsControl
子级时发生的事件:
PreviewMouseDown on (ContentPresenter)
MouseDown on (Rectangle)
MouseUp on (Rectangle)
请注意,预览事件在“ ContentPresenter”上,而不是在下一种情况下在“ TestScreen”上。
点击任何其他UserControl
时发生的事件:
PreviewMouseDown on (TestScreen)
MouseDown on (Ellipse)
MouseUp on (Ellipse)
一旦我添加了IsHitTestVisible
,那么两种情况看起来都是一样的(就像第二个例子一样)。
为什么ContentPresenter
的{{1}}而不是ItemsControl
预览了活动?在TestScreen
的视觉树下(我想是!)
这是TestScreen
的主体,它将抑制鼠标单击(稍后添加了UserControl
):
IsHitTestVisible
这是 <Canvas>
<ItemsControl
ItemsSource="{Binding Points}"
IsHitTestVisible="False"> <<=====
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate> <Canvas/> </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" ... />
<Setter Property="Canvas.Top" ... />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="10" Height="10" Margin="-5,-5">
<Rectangle.Style>
...
</Rectangle.Style>
</Rectangle>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
的正文,单击后没有任何问题:
UserControl
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)