Sending RoutedEvent from Code Behind for Custom UserControl
Posted: 10 Apr 2025, 16:56
Hello! I have a custom user control defined as `MyScreen` with the the following RoutedEvent `InitialFocus`. I am struggling to set up the EventTrigger in XAML to successfully catch this routed event. What might be going wrong? Is this behavior supported?
Then in the xaml I have the following EventTrigger to listen for the routed event
When the RoutedEvent is triggered using the following line in the code behind it doesn't actually get caught by the EventTrigger in xaml and focus is not set:
Code: Select all
public partial class MyScreen: UserControl
{
public static readonly RoutedEvent InitialFocusEvent= EventManager.RegisterRoutedEvent(
"InitialFocusEvent", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MyScreen));
public event RoutedEventHandler InitialFocus
{
add => AddHandler(InitialFocusEvent, value);
remove => RemoveHandler(InitialFocusEvent, value);
}
}
Code: Select all
<local:MyScreen>
<b:Interaction.Triggers>
<b:EventTrigger EventName="InitialFocus">
<noesis:SetFocusAction Engage="False" TargetName="MyButton"/>
</b:EventTrigger>
</b:Interaction.Triggers>
</local:MyScreen>
Code: Select all
RaiseEvent(new RoutedEventArgs(InitialFocusEvent, this));