Routed events in xaml

Nov 23, 2009 at 6:49 AM

Hi,

This is a really good addition to silverlight and one that is going to save me lots of pain!

However I am confused with one thing. I want to use the routed event to trigger an animation in xaml like this:

    <UserControl.Triggers>
        <EventTrigger RoutedEvent="local:PumpStationContainer.ValueChanged">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard Duration="00:00:0.5">
                        <DoubleAnimation Storyboard.TargetName="progressBar" Storyboard.TargetProperty="Value" To="10.0"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </UserControl.Triggers>

 and in the code I have this:

        #region Events

        public event RoutedEventHandlerEx ValueChanged
        {
            add { this.AddHandlerEx(ValueChangedEvent, value); }
            remove { this.RemoveHandlerEx(ValueChangedEvent, value); }
        }

        #endregion

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PumpStationContainer), new PropertyMetadata(0.0));
        public static readonly RoutedEventEx ValueChangedEvent = EventManagerEx.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandlerEx), typeof(PumpStationContainer));

        #region Object Lifetime

        public PumpStationContainer()
        {
            InitializeComponent();
        }

When InitializeComponent is called I get a XamlParseException thrown: Attribute local:PumpStationContainer.ValueChanged value is out of range. [Line: 14 Position: 35].

I think that it is clear that I cannot do it this way and I need to add the handler in code as in the example but I wasn't quite sure how to do this so that the handler is added to the event trigger in the triggers collection for the usercontrol.

Can anyone help me to identify what I am doing wrong?

Thanks

Ian