如何解决为什么这个ItemsControl ContentPresenter会“吃”鼠标单击事件?
我只是解决了一个问题,尽管找到了解决方法,但我不理解。
摘要: 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 举报,一经查实,本站将立刻删除。