Search code examples
c#wpfwpf-controlsmenuitemvisualstates

Use VisualStates with MenuItems to change Foreground color


I have what may seem like a relativly simple question but on that same note I am still relativly new to WPF so please me gental. My question is simply this, I have a VisualStateManager on my MenuItems in a context menu that I want to handle chaning the foreground color. Here is my attempt at it

           **WPF PORTION**

           <VisualStateManager.VisualStateGroups>
              <VisualStateGroup Name="ExcelVisualState">
                <VisualState Name="XLSXNormal">
                  <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Foreground" To="#FF003471" Duration="00:00:00.0010000" />
                  </Storyboard>
                </VisualState>
                <VisualState Name="XLSXDisabled">
                  <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Foreground" To="#A99B9A71" Duration="00:00:00.0010000" />
                  </Storyboard>
                </VisualState>
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

            **C# Code**
            //Fires when the isEnabled method changes for my menu item
            private void MenuItem_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
               //If MS Excel is installed set the visual state to XLSXNormal
               if (miExportXLSX.IsEnabled)
                  VisualStateManager.GoToState(miExportXLSX, "XLSXNormal", true);
               //MS Excel is not installed so set the state to XLSXDisabled
               else
                  VisualStateManager.GoToState(miExportXLSX, "XLSXDisabled", true);
            }

Am I on the right track here, or am I way off course? This is my first attempt at using Visual States, I know this may be a wee bit overkill for this simple of a task but I had to start somewhere and thought this would be easy enough.

(If any Clarification is required please let me know)


Solution

  • Try

    VisualStateManager.GoToElementState
    

    rather than

    VisualStateManager.GoToState