Tracking a button created programatically

Aug 5, 2010 at 7:29 AM
Edited Aug 5, 2010 at 7:30 AM

Hi All,

In my silverlight project a few of the buttons are created dynamically in code when the xaml is loaded. I want to track clicks on these buttons. As these buttons are not in the xaml(they are added dynamically), is there any way I can add triggers to these buttons in the C# code?

Thanks

Shivu

Coordinator
Aug 16, 2010 at 7:03 PM
  1. Add a reference to System.Windows.Interactivity, Microsoft.WebAnalytics, and Microsoft.WebAnalytics.Behaviors to the project
  2. Use this code to add the TrackAction to a new Button

var button = new Button();

var triggers = System.Windows.Interactivity.Interaction.GetTriggers(button);

var trigger = new System.Windows.Interactivity.EventTrigger()
            {
                EventName = "Click",
            };

trigger.Actions.Add(new Microsoft.WebAnalytics.Behaviors.TrackAction());

triggers.Add(trigger);

this.LayoutRoot.Children.Add(button);