/// <summary>
/// Sets focus to the next item in the ContextMenu.
/// </summary>
/// <param name="down">True to move the focus down; false to move it up.</param>
private void FocusNextItem(bool down)
{
int count = Items.Count;
int startingIndex = down ? -1 : count;
MenuItem focusedMenuItem = FocusManager.GetFocusedElement() as MenuItem;
if (null != focusedMenuItem && (this == focusedMenuItem.ParentMenuBase))
{
startingIndex = ItemContainerGenerator.IndexFromContainer(focusedMenuItem);
}
int index = startingIndex;
do
{
index = (index + count + (down ? 1 : -1)) % count;
MenuItem container = ItemContainerGenerator.ContainerFromIndex(index) as MenuItem;
if (null != container)
{
if (container.IsEnabled && container.Focus())
{
break;
}
}
}while (index != startingIndex);
}