Skip to main content

Microsoft Silverlight

Answered Question Firing events programmaticallyRSS Feed

(0)

vitya
vitya

Member

Member

111 points

75 Posts

Firing events programmatically

Hi,

Is there a way to fire control events programmatically? E.g. When the mouse hovers over a button, I want another button to play its MouseEnter animation.

Thanks

vitya

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP

Re: Firing events programmatically

No, even in C# you can't. But you can call directly the callback and passing the button you want as sender parameter.

vitya
vitya

Member

Member

111 points

75 Posts

Re: Re: Firing events programmatically

Thanks for the quick reply.

That way, though, I cannot start the animation :( or can I?

 vitya

pbromberg
pbromberg

Contributor

Contributor

2088 points

365 Posts

Re: Firing events programmatically

You don't need to put the code logic inside the control's eventhandler body. inside your eventhandler, you can make a call to a separate method. So for example in the mouse hover eventhandler, you can add a call to your method that does the MouseEnter animation. That is, this is what it sounds like you are asking for.

vitya
vitya

Member

Member

111 points

75 Posts

Re: Re: Firing events programmatically

But for the button the animations are defined with VSM. I don't have access to them from outside the button.

What am I missing?

vitya

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP

Re: Re: Firing events programmatically

I don't know if its a good practice but you can start the MouseOver animation with this code

yourButton.SetValue(Button.IsMouseOverProperty, true);

Maybe someone else has a bette solution.

vitya
vitya

Member

Member

111 points

75 Posts

Re: Re: Re: Firing events programmatically

Just tried it, but it doesn't seem to fire the MouseEnter event on the other button.

This is the code I use:

btn0.MouseEnter += new MouseEventHandler(btn0_MouseEnter);

btn4.MouseEnter += new MouseEventHandler(btn4_MouseEnter);

 

void btn0_MouseEnter(object sender, MouseEventArgs e)

{

 

}

 

void btn4_MouseEnter(object sender, MouseEventArgs e)

{

btn0.SetValue(
Button.IsMouseOverProperty, true);

}

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP

Re: Re: Re: Firing events programmatically

You said, you wanted the animation starts, and this is what my code does.

When you set the IsMouseOverProperty DP to true, the MouseOver animation defined in the template begins.

Is it what you want?

vitya
vitya

Member

Member

111 points

75 Posts

Re: Re: Re: Re: Firing events programmatically

yes, that's what I want, but it doesn't happen :(

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP
Answered Question

Re: Re: Re: Re: Firing events programmatically

Ok.

In fact it's easy

VisualStateManager.GoToState(button4, "MouseOver", true);

Just call the state you want.

vitya
vitya

Member

Member

111 points

75 Posts

Re: Re: Re: Re: Re: Firing events programmatically

Yes, this was it. Shame on me I didn't think of the simplest solution!

 Thank you for your support!

vitya

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities