In this article I’ll explain one way how to hide a cursor after n-seconds of inactivity though WPF behavior mechanism. Same effect you can observe on almost any video player — if you don’t touch a mouse for the few seconds, cursor will disappear.
First of all, what is a behavior?
A behavior encapsulates pieces of functionality into a reusable component, which we later on can attach to an element in a view. Emphasis is on reusable. Behaviors also have the benefit of keeping the MVVM pattern intact, since we can move code from code behind to behaviors. You can find a good introduction on the behaviors, type of behaviors in a great article of Robert Hedgate.
Implementation though attached behavior.
Advantage of attached behavior is that it can be used in a native windows store app. Disadvantage is that it does not show up in Blend under behaviors, and thus cannot be attached if you are using Blend. In my case it’s very important to have possibility of using it on windows store app.
In the implementation of the behavior we have 4 dependency properties:
- Interval — time interval after which cursor should be hided.
- IsEnabled — enable or disable behavior functionality.
- ActiveCursor — cursor type when it’s not active.
- InactiveCursor — cursor type when it’s active.
Logic isn’t really complicated so you just can download it from my Bitbucket.