0

I'd like to control which button is focused in my view. User must be able to perform a job without need to use mouse. And job is going through all elements. Depending on element, some buttons will appears, some disappears.

I could do it with dependency properties. To example, if there are buttons Previous and Next, then I can provide IsPreviousFocused and IsNextFocused to set focus to them after certain event. Events can be: showing window for the first time (something should have focus already), reaching 1 element (hiding Previous button, setting IsNextFocused), reaching last element (opposite), etc.

This looks reasonable more or less.

But, if I have, to example, 10 buttons (different operations), then all of them will have to have dependency property!

So I was thinking about much easier approach: when I hide button, there will be no focus

if(FocusManager.FocusedElement == null) { ... }

If I can detect, when there are no focus, then I can try to set it to one of the buttons somehow. This way, I don't really need any complicated focus management

Question is: how to deal with FocusManager.FocusedElement in MVVM scenario? How to detect when there is no focus (when window is shown first time, when certain button is clicked and become invisible, etc)?

P.S.: I actually hate ms for making another technology without thinking fully into it; focus is very basic feature (and everybody care about it in their software), but there is no direct support for it (in xaml); looks like "oh, its too complicated, lets skip it" solution.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • why not use keybindings or ALT+ _key? for your buttons? – blindmeis Mar 28 '14 at 08:04
  • @blindmeis, I myself do not like keybindings. Take to example some *multi-page wizard* (nsis installer), you can press `Enter` key all the way through it to get some result. Wizard itself can be started by keybinding, but operations inside are most of time `Next` (assuming default values are ok for you). This is something what I want to have. User will click `Next` most of times, but sometime he can click `Previous` or chose some option, this is where I need to manage focus (reaching 1 element, last element, first start, etc). – Sinatr Mar 28 '14 at 08:33

1 Answers1

1

You could control your focus from your ViewModel by using the approach shown here: Set focus on textbox in WPF from view model (C#)

Community
  • 1
  • 1
Simon D.
  • 4,451
  • 2
  • 28
  • 57
  • Was there already (and more likely in every *related* and *sub-related* topic). This is a scenario, where I have to define variable for **each** button individually. I am looking for something more generic, more elegant, *lesscody*. Ability to detect when there is no focus at all, to set it on something. – Sinatr Mar 27 '14 at 12:57
  • 2
    You can instead define a single string/enum variable in your ViewModel, which 'names' the focussed element. Then for each Button, you can bind the AttachedProperty to this variable using a ValueConverter with a ConverterParameter => each Button uses a different ConverterParameter. The ValueConverter should return true if the ConverterParameter is equal to the value (the ViewModel-variable). – Simon D. Mar 27 '14 at 14:59