Search code examples
c#silverlighteventseventtrigger

Silverlight custom EventTrigger


I am trying to create a custom event to trigger an animation in Silverlight. Although the event is getting triggered, the animation is not working. The following is the relevant code:

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            MyEvent += new ChangedEventHandler(UserControl_MyEventHandler);
            /* Other stuff */
        }
        private void UserControl_MyEventHandler(object sender, RoutedEventArgs e)
        {
                MessageBox.Show("MyEventHandler has been called");
        }

        public delegate void ChangedEventHandler(object sender, RoutedEventArgs e);

        private event ChangedEventHandler MyEvent;

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
                if (MyEvent != null)
                    MyEvent(this, e);
        }
    }
}

The XAML code is as follows:

<UserControl
... 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d"
x:Class="SilverlightApplication1.MainPage" MouseLeftButtonDown="UserControl_MouseLeftButtonDown">

    <Grid x:Name="LayoutRoot" Background="White">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MyEvent">
                <ei:GoToStateAction StateName="Highlighted"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        ...
    </Grid>
</UserControl>

Current, the message box containing "MouseLeftButtonDown" is getting displayed but the animation is not getting called. The animation did get called when the EventTrigger EventName was MouseLeftButtonDown instead of MyEvent. Please help me out. Thanks.


Solution

  • You can set SourceName in EventTrigger if you want to trigger on event of some control. I guess, in your case in code behind:

    VisualStateManager.GoToState(this [or some other object with Highlighted state], "Highlighted", false);