Why DataBinding is not propagating to UserControl

回眸只為那壹抹淺笑 提交于 2019-12-01 15:02:35

The reason that you are not getting commands invoked on your user control is that your buttons are not in separate focus scope. For WPF to pick up focused element for command target correctly, it needs to be in separate focus scope from command invoking control.

Framework will just traverse up visual tree from button looking for command bindings in their focus scope (in your case it won't find any). When framework does not find any command bindings in current focus scope, only then it looks into parent focus scope for focused element (In your case, buttons are in Window focus scope which has no parent scope so the search will end there).

Simply setting FocusManager.IsFocusScope="True" on your StackPanel will fix the issue.

You could also specify CommandTarget property on your buttons to point to your user control and not rely on focus.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!