Setting Focus on a Control within a ControlTemplate in WPF

前端 未结 2 2022
半阙折子戏
半阙折子戏 2021-01-14 05:26

In an application I\'m working on, we have a bunch of custom controls with their ControlTemplates defined in Generic.xaml.

For instance, our custom textbox would loo

相关标签:
2条回答
  • 2021-01-14 05:54

    You can get rid of the hard coding of control name in the code by providing some DependancyProperty and have the same code in controlLoaded or OnApplyTemplate function based on the DependancyProperty. This DependancyProperty's sender will the candidate for .Focus() call.

    0 讨论(0)
  • 2021-01-14 05:59

    Within your control template you can add a Trigger that sets the FocusedElement of the StackPanel's FocusManager to the textbox you want focused. You set the Trigger's property to {TemplateBinding IsFocused} so it fires when the containing control is focused.

    0 讨论(0)
提交回复
热议问题