Help setting focus to element in ItemsControl
Posted: 10 Feb 2025, 18:41
Hi Noesis fokes,
I'm trying to set the default focus to the first element of an ItemControl, on load and if the items change. However, my below code doesn't work.
element->Focus() and element->MoveFocus(request) return false. The end result is no items have focus.
Please advise on what I'm doing wrong.
Thanks in advanced!
Update: I have found that the above works if I add a very small timer to set the focus after a few ms. Any thought on how I can get round this hack would be great :D
I'm trying to set the default focus to the first element of an ItemControl, on load and if the items change. However, my below code doesn't work.
element->Focus() and element->MoveFocus(request) return false. The end result is no items have focus.
Please advise on what I'm doing wrong.
Thanks in advanced!
Code: Select all
MAUserControl::MAUserControl() : UserControl()
{
Loaded() += Noesis::MakeDelegate(this, &MAUserControl::OnLoaded);
}
void MAUserControl::OnLoaded(Noesis::BaseComponent*, const Noesis::RoutedEventArgs&)
{
const char* focus_name = GetValue<Noesis::String>(DefaultFocusElementNameProperty).Str();
if (focus_name != nullptr)
{
Noesis::BaseComponent* focus_child = FindName(focus_name);
if (focus_child != nullptr)
{
Noesis::ItemsControl* item_control = Noesis::DynamicCast<Noesis::ItemsControl*>(focus_child);
if (item_control != nullptr)
{
Noesis::ItemCollection* items = item_control->GetItems();
if (items != nullptr)
{
items->CollectionChanged() += Noesis::MakeDelegate(this, &MAUserControl::OnItemsChaged);
}
}
}
}
SetDefaultFocus();
}
void MAUserControl::OnItemsChaged(Noesis::BaseComponent*, const Noesis::NotifyCollectionChangedEventArgs&)
{
SetDefaultFocus();
}
void MAUserControl::SetDefaultFocus()
{
const char* focus_name = GetValue<Noesis::String>(DefaultFocusElementNameProperty).Str();
if (focus_name != nullptr)
{
Noesis::BaseComponent* focus_child = FindName(focus_name);
if (focus_child != nullptr)
{
Noesis::ItemsControl* item_control = Noesis::DynamicCast<Noesis::ItemsControl*>(focus_child);
if (item_control != nullptr && item_control->GetHasItems())
{
Noesis::ItemContainerGenerator* generator = item_control->GetItemContainerGenerator();
Noesis::UIElement* element = Noesis::DynamicCast<Noesis::UIElement*>(generator->ContainerFromIndex(0));
if (element != nullptr)
{
if (element->Focus() || element->GetIsKeyboardFocusWithin())
{
return;
}
// container is not focusable, try to focus its contents
Noesis::TraversalRequest request = { Noesis::FocusNavigationDirection_First, false };
if (element->MoveFocus(request))
{
return;
}
Noesis::DependencyObject* focusScope = Noesis::FocusManager::GetFocusScope(element);
if (focusScope != nullptr && focusScope != element)
{
Noesis::FocusManager::SetFocusedElement(focusScope, element);
return;
}
}
}
}
}
}
Code: Select all
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Height="50" Margin="0 0 0 5"
DockPanel.Dock="Bottom"
Style="{StaticResource ButtonStyleRectangular_Resize}"
Content="{Binding PlayerChoiceText}">
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>