Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone know if there is a way to deselect everything on a WPF window? I've seen some applications do something similar, often when pressing the escape key. What it appears to do is to put the window back in the state when it was first loaded, before the user clicks anything or tabs to a control on the window.

What I have tried:

Nothing really because so far I haven't found anything that looks like it will do anything I need.

I am thinking of maybe trying to have an invisible control and setting focus to that, but this feels like a complete hack.
Posted
Updated 19-Sep-16 2:45am
Comments
Suvendu Shekhar Giri 19-Sep-16 8:38am    
deselect everything means?
Which controls/what are selected here?
Patrick Skelton 19-Sep-16 9:08am    
It is a set of TextBox controls inside a Grid. It just would feel more natural if pressing Esc or clicking on a 'blank' area of the window removed selection from whichever TextBox currently has keyboard focus.

1 solution

That would be a complete hack because you loose control over what gets deselected if this requirement ever changes.

The proper solution is to write the code to go through the controls and clear the selections however it needs to be done for each. It's not the sexiest solution but it's maintainable and debugable.

You can then call this method from an event handler or from some other code if required.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900