MouseLeftButtonDown is not fired on TreeViewItem

烈酒焚心 提交于 2019-12-11 02:41:59

问题


<Grid x:Name="LayoutRoot" Background="White">
    <sdk:TreeView MouseLeftButtonDown="TreeView_MouseLeftButtonDown">
        <sdk:TreeViewItem Header="this is first item"/>
    </sdk:TreeView>
</Grid>


call is not coming to TreeView_MouseLeftButtonDown event handler.. any ideas or work around?


回答1:


Have you ever noticed that the MouseLeftButtonDown and MouseLeftButtonUp events are not fired when a Silverlight button is clicked? The reason for this is that the button handles these two events itself by overriding the OnMouseLeftButtonDown and the OnMouseLeftButtonUp handlers. In the OnMouseLeftButtonDown override, the Click event is raised and the MouseLeftButtonDown event is marked as handled so it couldn't bubble in the visual tree. The OnMouseLeftButtonUp override also marks the MouseLeftButtonUp as handled.

This thing can be changed using the ClickMode property of the Button control. It has the following values - Hover, Press, Release. The default one is Pressed and we have already explained it. When we have ClickMode set to Release, the Click event will be raised in the OnMouseLeftButtonUp override and the MouseLeftButtonDown and MouseLeftButtonUp events will be handled inside the button again. If we set the ClickMode to Hover, the Click event will be raised with the MouseEnter event and we will also be able to use the mouse button events.




回答2:


The reason behind this is because TreeView handles such event as well. To override such handling, you must handle it in the code using the below:

treeItem.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(treeItem_MouseLeftButtonDown), true);


来源:https://stackoverflow.com/questions/14851305/mouseleftbuttondown-is-not-fired-on-treeviewitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!