WPF Event raised by UI or from code?

血红的双手。 提交于 2019-12-12 02:17:45

问题


how can I find if an event is raised by UI or by code? I need to recognaize if Value of a slider is changed by the user from UI or has a new value from the executed code?

Thanks!


回答1:


I'm assuming that you've got your Slider hooked up to a two-way binding where the current value of the slider could thus be determined either by the user dragging the slider Thumb or by the bound class's code changing the value of the property that the slider is bound to. If this is true, then I think an approach that could work is if you use a handler for the Slider Thumb's DragCompleted event. Although I've not tried this, I believe this will work because it would seem that the Thumb will only be "dragged" by the user, rather than your executing code changing the value that the slider is bound to (although I concede that this will cause the Thumb to "move" so this will require testing to verify). You will register your handler for this event in the constructor in your code-behind.

See the answers by YotaXP and Santo for some more guidance from this similar question.




回答2:


As Thumb's DragCompleted event does not address the case that user clicks on slider, I used something similar to what Peter suggesetd in link, I used PreviewMouseLeftButtonUp event of the slider. This addresses all the cases.



来源:https://stackoverflow.com/questions/14630306/wpf-event-raised-by-ui-or-from-code

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