質問

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)

役に立ちましたか?

解決

Try

VisualStateManager.GoToElementState

rather than

VisualStateManager.GoToState

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top