locked
Border is not a control? Need help Casting... RRS feed

  • Question

  •   I have several buttons with a border around each one, and when I mouseenter/mouseleave the border, I want some animations to fire off that show the border highlighting. I created these animations in Blend 4 using the VisualStateManager (VSM) and now I just need to wire them up.

    Pretty standard stuff here, so here is the Border xaml:

    Snippet

     
    <Border CornerRadius="15" Background="#00FFA500" MouseEnter="OnMouseOver" MouseLeave="OnMouseOut">
     

     And here is the code behind:

    Snippet

     
            private void OnMouseOver(object sender, System.Windows.Input.MouseEventArgs e)
    {
    VisualStateManager.GoToState((Control)sender, "BorderHighlight", true);
    }
     
    All well and good until I try to actually run it - when the mouse enters the border, I get this error message:

    Unable to cast object of type 'System.Windows.Controls.Border' to type 'System.Windows.Controls.Control'.


    How something in the System.Windows.Controls namespace is not a Control is beyond me, but there ya go.

    So... how do I fix this? (Note that the GoToState() signature expects a type of Control as its first parameter)
    Saturday, June 19, 2010 9:04 AM

Answers

  • You fix it by not attempting to use the VisualStateManager on something which isn't a control. Border isn't a control, it's more primitive than that. Things that derive from Control are controls. So, determine why you're trying to use the visual state manager on a border, and change your design so that you no longer attempt to do that.

    Saturday, June 19, 2010 1:15 PM