yossharel
yossharel

Reputation: 1929

How to set focus on element on ItemsControl?

I've ItemsControl that its ItemsSource property bind to some dictionary from code behind. The ItemTemplate is consist of only one button. So, for each item in dictionary it creates button.

My question is how can I set focus to one of these buttons (dynamically)? Should I use ItemContainerGenerator.ContainerFromItem ? Any other idea?

Thanks in advance!

Upvotes: 1

Views: 4498

Answers (1)

Pavlo Glazkov
Pavlo Glazkov

Reputation: 20746

Yes, use can ItemContainerGenerator.ContainerFromItem to get a container for your data item, then you will need to find you button inside this container and call Focus() on the button.

OR you can use an attached property to bind IsFocused to a property on your data item. See Set focus on textbox in WPF from view model (C#)

Upvotes: 5

Related Questions