<p>I'm trying to trigger a progress animation when ever the ViewModel/Presentation Model is Busy. I have an IsBusy Property, and the ViewModel is set as the DataContext of the UserControl. What is the best way to trigger a &quot;progressAnimation&quot; storyboard when the IsBusy property is true? Blend only lets me add event triggers at the UserControl level, and I can only create property triggers in my data templates.</p>
<p>The &quot;progressAnimation&quot; is defined as a resource in the user control.</p>
<p>I tried adding the DataTriggers as a Style on the UserControl, but when I try to start the StoryBoard I get the following error:</p>
<blockquote>
<p>'System.Windows.Style' value cannot be assigned to property 'Style'
of object'Colorful.Control.SearchPanel'. A Storyboard tree in a Style
cannot specify a TargetName. Remove TargetName 'progressWheel'.</p>
</blockquote>
<p>ProgressWheel is the name of the object I'm trying to animate, so removing the target name is obviously NOT what I want.</p>
<p>I was hoping to solve this in XAML using data binding techniques, instead of having to expose events and start/stop the animation through code.</p>

[{'answer_id': 80455, 'author': 'Jobi Joy', 'author_id': 8091, 'author_profile': 'https://Stackoverflow.com/users/8091', 'pm_score': 1, 'selected': False, 'text': '<p>I would recommend to use RoutedEvent instead of your IsBusy property. Just fire OnBusyStarted and OnBusyStopped event and use Event trigger on the appropriate elements.</p>\n'}, {'answer_id': 80794, 'author': 'Jobi Joy', 'author_id': 8091, 'author_profile': 'https://Stackoverflow.com/users/8091', 'pm_score': 1, 'selected': False, 'text': '<p>You can subscribe to the PropertyChanged event of the DataObject class and make a  RoutedEvent fire from Usercontrol level. </p>\n\n<p>For RoutedEvent to work we need to have the class derived from DependancyObject</p>\n'}, {'answer_id': 81175, 'author': 'ligaz', 'author_id': 6409, 'author_profile': 'https://Stackoverflow.com/users/6409', 'pm_score': 0, 'selected': False, 'text': '<p>You can use Trigger.EnterAction to start an animation when a property is changed.</p>\n\n<pre><code>&lt;Trigger Property="IsBusy" Value="true"&gt;\n    &lt;Trigger.EnterActions&gt;\n        &lt;BeginStoryboard x:Name="BeginBusy" Storyboard="{StaticResource MyStoryboard}" /&gt;\n    &lt;/Trigger.EnterActions&gt;\n    &lt;Trigger.ExitActions&gt;\n        &lt;StopStoryboard BeginStoryboardName="BeginBusy" /&gt;\n    &lt;/Trigger.ExitActions&gt;\n&lt;/Trigger&gt;\n</code></pre>\n'}, {'answer_id': 1735810, 'author': 'Dabblernl', 'author_id': 108493, 'author_profile': 'https://Stackoverflow.com/users/108493', 'pm_score': 7, 'selected': True, 'text': '<p>What you want is possible by declaring the animation on the progressWheel itself:\nThe XAML:</p>\n\n<pre><code>&lt;UserControl x:Class="TriggerSpike.UserControl1"\nxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\nxmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\nHeight="300" Width="300"&gt;\n&lt;UserControl.Resources&gt;\n    &lt;DoubleAnimation x:Key="SearchAnimation" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:4"/&gt;\n    &lt;DoubleAnimation x:Key="StopSearchAnimation" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:4"/&gt;\n&lt;/UserControl.Resources&gt;\n&lt;StackPanel&gt;\n    &lt;TextBlock Name="progressWheel" TextAlignment="Center" Opacity="0"&gt;\n        &lt;TextBlock.Style&gt;\n            &lt;Style&gt;\n                &lt;Style.Triggers&gt;\n                    &lt;DataTrigger Binding="{Binding IsBusy}" Value="True"&gt;\n                        &lt;DataTrigger.EnterActions&gt;\n                            &lt;BeginStoryboard&gt;\n                                &lt;Storyboard&gt;\n                                    &lt;StaticResource ResourceKey="SearchAnimation"/&gt;\n                                &lt;/Storyboard&gt;\n                            &lt;/BeginStoryboard&gt;\n                        &lt;/DataTrigger.EnterActions&gt;\n                        &lt;DataTrigger.ExitActions&gt;\n                            &lt;BeginStoryboard&gt;\n                                &lt;Storyboard&gt;\n                                   &lt;StaticResource ResourceKey="StopSearchAnimation"/&gt; \n                                &lt;/Storyboard&gt;\n                            &lt;/BeginStoryboard&gt;\n                        &lt;/DataTrigger.ExitActions&gt;\n                    &lt;/DataTrigger&gt;\n                &lt;/Style.Triggers&gt;\n            &lt;/Style&gt;\n        &lt;/TextBlock.Style&gt;\n        Searching\n    &lt;/TextBlock&gt;\n    &lt;Label Content="Here your search query"/&gt;\n    &lt;TextBox Text="{Binding SearchClause}"/&gt;\n    &lt;Button Click="Button_Click"&gt;Search!&lt;/Button&gt;\n    &lt;TextBlock Text="{Binding Result}"/&gt;\n&lt;/StackPanel&gt;\n</code></pre>\n\n<p></p>\n\n<p>Code behind:</p>\n\n<pre><code>    using System.Windows;\nusing System.Windows.Controls;\n\nnamespace TriggerSpike\n{\n    public partial class UserControl1 : UserControl\n    {\n        private MyViewModel myModel;\n\n        public UserControl1()\n        {\n            myModel=new MyViewModel();\n            DataContext = myModel;\n            InitializeComponent();\n        }\n\n        private void Button_Click(object sender, RoutedEventArgs e)\n        {\n            myModel.Search(myModel.SearchClause);\n        }\n    }\n}\n</code></pre>\n\n<p>The viewmodel:</p>\n\n<pre><code>    using System.ComponentModel;\nusing System.Threading;\nusing System.Windows;\n\nnamespace TriggerSpike\n{\n    class MyViewModel:DependencyObject\n    {\n\n        public string SearchClause{ get;set;}\n\n        public bool IsBusy\n        {\n            get { return (bool)GetValue(IsBusyProperty); }\n            set { SetValue(IsBusyProperty, value); }\n        }\n\n        public static readonly DependencyProperty IsBusyProperty =\n            DependencyProperty.Register("IsBusy", typeof(bool), typeof(MyViewModel), new UIPropertyMetadata(false));\n\n\n\n        public string Result\n        {\n            get { return (string)GetValue(ResultProperty); }\n            set { SetValue(ResultProperty, value); }\n        }\n\n        public static readonly DependencyProperty ResultProperty =\n            DependencyProperty.Register("Result", typeof(string), typeof(MyViewModel), new UIPropertyMetadata(string.Empty));\n\n        public void Search(string search_clause)\n        {\n            Result = string.Empty;\n            SearchClause = search_clause;\n            var worker = new BackgroundWorker();\n            worker.DoWork += worker_DoWork;\n            worker.RunWorkerCompleted += worker_RunWorkerCompleted;\n            IsBusy = true;\n            worker.RunWorkerAsync();\n        }\n\n        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)\n        {\n            IsBusy=false;\n            Result = "Sorry, no results found for: " + SearchClause;\n        }\n\n        void worker_DoWork(object sender, DoWorkEventArgs e)\n        {\n            Thread.Sleep(5000);\n        }\n    }\n}\n</code></pre>\n\n<p>Hope this helps!</p>\n'}, {'answer_id': 24774823, 'author': 'Ian Griffiths', 'author_id': 497397, 'author_profile': 'https://Stackoverflow.com/users/497397', 'pm_score': 4, 'selected': False, 'text': '<p>Although the answer that proposes attaching the animation directly to the element to be animated solves this problem in simple cases, this isn\'t really workable when you have a complex animation that needs to target multiple elements. (You can attach an animation to each element of course, but it gets pretty horrible to manage.)</p>\n\n<p>So there\'s an alternative way to solve this that lets you use a <code>DataTrigger</code> to run an animation that targets named elements.</p>\n\n<p>There are three places you can attach triggers in WPF: elements, styles, and templates. However, the first two options don\'t work here. The first is ruled out because WPF doesn\'t support the use of a <code>DataTrigger</code> directly on an element. (There\'s no particularly good reason for this, as far as I know. As far as I remember, when I asked people on the WPF team about this many years ago, they said they\'d have liked to have supported it but didn\'t have time to make it work.) And styles are out because, as the error message you\'ve reported says, you can\'t target named elements in an animation associated with a style.</p>\n\n<p>So that leaves templates. And you can use either control or data templates for this.</p>\n\n<pre><code>&lt;ContentControl&gt;\n    &lt;ContentControl.Template&gt;\n        &lt;ControlTemplate TargetType="ContentControl"&gt;\n            &lt;ControlTemplate.Resources&gt;\n                &lt;Storyboard x:Key="myAnimation"&gt;\n\n                    &lt;!-- Your animation goes here... --&gt;\n\n                &lt;/Storyboard&gt;\n            &lt;/ControlTemplate.Resources&gt;\n            &lt;ControlTemplate.Triggers&gt;\n                &lt;DataTrigger\n                    Binding="{Binding MyProperty}"\n                    Value="DesiredValue"&gt;\n                    &lt;DataTrigger.EnterActions&gt;\n                        &lt;BeginStoryboard\n                            x:Name="beginAnimation"\n                            Storyboard="{StaticResource myAnimation}" /&gt;\n                    &lt;/DataTrigger.EnterActions&gt;\n                    &lt;DataTrigger.ExitActions&gt;\n                        &lt;StopStoryboard\n                            BeginStoryboardName="beginAnimation" /&gt;\n                    &lt;/DataTrigger.ExitActions&gt;\n                &lt;/DataTrigger&gt;\n            &lt;/ControlTemplate.Triggers&gt;\n\n            &lt;!-- Content to be animated goes here --&gt;\n\n        &lt;/ControlTemplate&gt;\n    &lt;/ContentControl.Template&gt;\n&lt;ContentControl&gt;\n</code></pre>\n\n<p>With this construction, WPF is happy to let the animation refer to named elements inside the template. (I\'ve left both the animation and the template content empty here - obviously you\'d populate that with your actual animation nd content.)</p>\n\n<p>The reason this works in a template but not a style is that when you apply a template, the named elements it defines will always be present, and so it\'s safe for animations defined within that template\'s scope to refer to those elements. This is not generally the case with a style, because styles can be applied to multiple different elements, each of which may have quite different visual trees. (It\'s a little frustrating that it prevents you from doing this even in scenarios when you can be certain that the required elements will be there, but perhaps there\'s something that makes it very difficult for the animation to be bound to the named elements at the right time. I know there are quite a lot of optimizations in WPF to enable elements of a style to be reused efficiently, so perhaps one of those is what makes this difficult to support.)</p>\n'}]
['https://Stackoverflow.com/questions/80388', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1199387/']