WPF Event raised by UI or from code?

2019-09-08 05:19发布

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!

2条回答
女痞
2楼-- · 2019-09-08 05:44

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.

查看更多
Juvenile、少年°
3楼-- · 2019-09-08 05:47

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.

查看更多
登录 后发表回答