Silverlight custom EventTrigger

本文关键字:EventTrigger custom Silverlight | 更新日期: 2023-09-27 18:06:42

我正在尝试创建一个自定义事件来触发Silverlight中的动画。虽然事件被触发了,但是动画没有工作。以下是相关代码:

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);
        }
    }
}
XAML代码如下:
<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>

当前,包含"MouseLeftButtonDown"的消息框正在显示,但动画没有被调用。当EventTrigger EventName是MouseLeftButtonDown而不是MyEvent时,动画被调用。请帮帮我。谢谢。

Silverlight custom EventTrigger

如果你想触发某个控件的事件,可以在EventTrigger中设置SourceName。我猜,在你的情况下,在代码后面:

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