我们正在尝试使用GettingFocus将焦点从祖先控件重定向到子节点,但我们看到ArgumentExceptions在某些情况下被抛出。
到目前为止我注意到的案例:
无论如何都要保护args.NewFocusedElement = element; ?
我们将异常作为临时解决方案。
以下是代码:
private void OnGettingFocus(UIElement sender, GettingFocusEventArgs args)
{
if ((args.Direction == FocusNavigationDirection.Up || args.Direction == FocusNavigationDirection.Down)
&& (sender == this) && (DefaultFocusButton != null))
// Add extra guard here so that the try / catch is no longer needed
{
try
{
args.NewFocusedElement = DefaultFocusButton;
}
catch (ArgumentException e)
{
// This focus redirect causes an exception when the element can't receive focus
}
}
}