1

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)

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jimmy
  • 941
  • 2
  • 8
  • 27
  • Looks like you are on the right track. Does it behave correctly? – Ashley Grenon Jun 03 '13 at 20:40
  • No it does not. I have tried using Hex Codes and regular colors 'Red', 'Cyan', ect. but the color never changes. I also just realized I left out the C# portion of the code I will add that now. – Jimmy Jun 03 '13 at 20:43
  • So this will only be in the disabled state if you don't have excel installed? – Ashley Grenon Jun 03 '13 at 20:50
  • Correct and I am running on a VM that does not have MS Excel installed. The menu items are using an application to export something to an XLSX file or a PDF file and I want those options to be disabled if the user does not have them installed. They are being disabled so I know that this section is working, but that is done elsewhere. This section is strictly to change the color or the MenuItem so that it is visible that the option is disabled. – Jimmy Jun 03 '13 at 20:52

2 Answers2

1

I noticed you are not setting Storyboard.TargetName

Try setting this to the appropriate control name.

EDIT:

I think I see it now. Your color animation's target property: Instead of setting it to "Foreground" change it to "Color". If you do this you'll have to change the target control from MenuItem to the MenuItem's background brush.

Or you can leave that as is and change the target property in the ColorAnimation to Foreground.SolidColorBrush.Color.

Here's an example of setting the target property of the ColorAnimation.

Ashley Grenon
  • 9,305
  • 4
  • 41
  • 54
1

Try

VisualStateManager.GoToElementState

rather than

VisualStateManager.GoToState
Richard E
  • 4,819
  • 1
  • 19
  • 25
  • This fixed my problem! However only after I implemented what @townsean had suggested so I am not sure how to mark this as an answer because I had to use both options to get to my final answer... – Jimmy Jun 04 '13 at 13:36
  • 1
    I am marking this as the answer only because this was the main step I needed to take to get this working but I do want to give @townsean a good bit of credit as well because he was extremley helpful and informative. Thank you to both of you. – Jimmy Jun 04 '13 at 20:50