title: Change style in ModelView (MVVM + WPF)
body: <p>I have an application developed in WPF using the MVVM pattern (MVVM Light Toolkit).</p>
  
  <p>So far, I had no problems, until it is time to change at runtime the style associated with some of my controls, a set of MenuItems. (They can have up to three different styles).</p>
  
  <p>If I was not working with MVVM I could solve it using the command:    </p>
  
  <pre><code>MenuElement_Left4.Style = (Style)FindResource("MenuButtonTabsLeft");
  </code></pre>
  
  <p>But because I want do it completely in MVVM, I've done these tests to achieve that:</p>
  
  <p>1) Try to change the style with a binding (this has not worked):</p>
  
  <pre><code>&lt;MenuItem x:Name="MenuElement_Left4" Header="Test" Style="{Binding SelectedStyle}"&gt;
  </code></pre>
  
  <p>And in the ViewModel:</p>
  
  <pre><code>public string SelectedStyle
      {
         get { return this.selectedStyle; }
         set { this.selectedStyle = value; 
               RaisePropertyChanged("SelectedStyle");
             }
      }
      private string selectedStyle;
  </code></pre>
  
  <p>2) Change the style with DataTrigger (this has not worked too. Raises an exception (<a href="https://stackoverflow.com/questions/7519792/style-trigger-to-apply-another-style">Style Trigger to Apply another Style</a>)):</p>
  
  <pre><code>&lt;MenuItem.Style&gt;
      &lt;Style TargetType="{x:Type MenuItem}"&gt;
          &lt;Style.Triggers&gt;
              &lt;DataTrigger Binding="{Binding Path=TestStyle}" Value="True"&gt;
                  &lt;Setter Property="Style" Value="{StaticResource  MenuButtonTabsLeftArrow}"/&gt;
              &lt;/DataTrigger&gt;
              &lt;DataTrigger Binding="{Binding Path=TestStyle}" Value="False"&gt;
                  &lt;Setter Property="Style" Value="{StaticResource  MenuButtonTabsLeft}"/&gt;
              &lt;/DataTrigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/MenuItem.Style&gt;
  </code></pre>
  
  <hr>
  
  <p>At the end, I managed to solve it, using a Combox using the following code (I only use the ComboBox to change the style of the MenuItems so it is invisible). Got the idea from (<a href="https://stackoverflow.com/questions/1878046/how-can-i-change-an-elements-style-at-runtime">How can I change an elements style at runtime?</a>):</p>
  
  <pre><code>&lt;MenuItem x:Name="MenuElement_Left4" Header="Test" Style="{Binding ElementName=AvailableStyles, Path=SelectedItem.Tag}"&gt;
  &lt;ComboBox Name="AvailableStyles" SelectedIndex="{Binding AvailableStylesIndex}" Visibility="Collapsed"&gt;
      &lt;ComboBoxItem Tag="{x:Null}"&gt;None&lt;/ComboBoxItem&gt;
      &lt;ComboBoxItem Tag="{StaticResource MenuButtonTabsLeftArrow}"&gt;MenuButtonTabsLeftArrow&lt;/ComboBoxItem&gt;
      &lt;ComboBoxItem Tag="{StaticResource MenuButtonTabsLeft}"&gt;MenuButtonTabsLeft&lt;/ComboBoxItem&gt;
  &lt;/ComboBox&gt;
  </code></pre>
  
  <p>And in my ViewModel:</p>
  
  <pre><code>public int AvailableStylesIndex
  {
      get { return this.availableStylesIndex; }
      set
      {
          this.availableStylesIndex = value;
          RaisePropertyChanged("AvailableStylesIndex");
      }
  }
  </code></pre>
  
  <p>I'd rather use a cleaner way. Any suggestions? A piece of code would be very helpful.</p>