0
votes

I used to have a window with an attached property to track the focused control:

FocusManager.FocusedElement="{Binding CurrentlySelectedTextBox, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

CurrentlySelectedTextBox was a property in the window's viewmodel.

Now my window has a frame. The content of the frame is a page. I moved the original window's content to the page with its own viewmodel. I moved the attached property from the window to the page. Since then the CurrentlySelectedTextBox property is never set.

Long days of searching gave nothing. Are pages immune to FocusManager.FocusedElement?

Update: If the attached property is on the window, FocusManager.FocusedElement gets set even if the focus is on the page's control.

1
I think your problem is probably focus scope. But that's not why I'm commenting. Don't use pages. They were invented way back when wpf was being built and wpf in a browser was a thing. You'll find hosting directly in a window very limiting and probably then realise you need a Frame. Which comes with a bunch of memory pitfalls to catch you out. Use usercontrols instead and host in a contentcontrol. social.technet.microsoft.com/wiki/contents/articles/…Andy
@Andy I'm a bit at a loss because I follow the WPF series by AngelSix on YouTube (who knows so much as if he were the creator of WPF), and that's the approach he used. Btw currently my window has a frame. The content of the frame is a page. Does it save me from problems?Quasimodo
Don't use frames and pages. Follow the link in my comment above.Andy
@Andy Thank you for the advice. I completely switched to using UserControls and abandoned both pages and frames, and I guess I never needed the navigation stuff. As for the Focus Scope, you were also correct. Would you post that as an answer?Quasimodo

1 Answers

1
votes

There's a concept of focusscope in wpf which can cause issues like this.

The basic problem is your page is a different focusscope to the window.

Focusscope allows wpf to maintain a logical rather than physical concept of focus for controls such as tabcontrol and menu.

In order to maintain which has logical focus the focusmanager will by default be concerned with the element which has focus within each focusscope.

The behaviour is somewhat configurable but you need to be very careful before altering this as there are side effects.