I have a bunch of controls using identical interactions triggers across multiple user controls and viewmodels. Is it possible to place these triggers somehow into a resource dictionary for reuse? Here’s an example of what a control might look like.
<TextBox x:Name="FirstName" Grid.Row="1" Grid.Column="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cal:ActionMessage MethodName="KeyPressed" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBox x:Name="Initial" Grid.Row="1" Grid.Column="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cal:ActionMessage MethodName="KeyPressed" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBox x:Name="LastName" Grid.Row="1" Grid.Column="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cal:ActionMessage MethodName="KeyPressed" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
The cal: namespace is from the Caliburn.Micro MVVM framework and probably isn’t relevant to this question.
Its not possible to re-use a single instance of
Interaction.Triggersin a resource because it becomes attached a control. That attachment becomes part of its state hence a single instance can’t be shared by multiple controls.You would need to include the
Interaction.Triggersin a template so that multiple instances are created. I guess something like the following might work, (warning air code).…
It my opinion that this sort of stuff isn’t worth it. The Interaction Triggers stuff is really aimed at empowering the designer rather than the developer. A designer isn’t that worried that there is some repeatition in the “code”.