A way to use triggers anywhere in WPF

The biggest limitation of triggers in WPF is that except for EventTrigger they only work in styles and templates, I always find myself in situation where just putting a trigger directly in my window or control class will save me a lot of work – but it just can’t be done.

Just a few days ago I accidently run across a simple workaround, this is probably the best kept secret in WPF and is hidden in a little blue box at the end of this post on animation in M-V-VM.

Just define a DataTemplate with whatever you want to show and your triggers (DataTemplate supports all trigger types: Trigger, DataTrigger and EventTrigger) and drop the data template anywhere by using this XAML:

<ContentPresenter ContentTemplate="{StaticResource myTemplate}" Content="{Binding}" />

posted @ Monday, March 16, 2009 5:06 PM

Comments on this entry:

# re: A way to use triggers anywhere in WPF

Left by Festyx at 4/4/2009 7:10 AM

Sweet tip! That will be very useful. Thankyou.

# re: A way to use triggers anywhere in WPF

Left by software development company at 10/8/2009 1:16 PM

Hey, that was interesting,

great tips, really useful

Keep up the good work

Your comment:

 (will not be displayed)

Please add 6 and 6 and type the answer here: