none
Firing event of the object added on itemcontrol from CodeBehind

    Question

  • Hi

    Can anyone help me solving this problem?

    I have button control that is added on itemcontrol from codebehind but event doesn't fire.

    My code is below.

     

    Dim btn As New Button

    AddHandler btn.MouseLeftButtonUp, AddressOf AddNew_MouseLeftButtonUp

    my_itemcontrol.Items.Add(btn)

    Private Sub AddNew_MouseLeftButtonUp(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)

    End Sub

     

    Thank you in advance.

    Tuesday, March 27, 2012 6:17 AM

All replies

  • hi ,

    I tried adding a button from code  to a listbox.    I found that the mouseLeftButtonUp was not firing, as it is just a mouse button event, whereas  click event was firing as it is a routed event. 

    So making use of a routed event handler would solve your problem .

    Here's my code

      public MainPage()
            {
                InitializeComponent();
               
                Button b = new Button();
                b.Width = 30;
                b.Height = 30;
                b.Click+=new RoutedEventHandler(b_Click);
                b.MouseLeftButtonUp += new MouseButtonEventHandler(b_MouseLeftButtonUp);
                listBox1.Items.Add(b);
             
            }

            void b_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                MessageBox.Show("hi left");
            }

            void b_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("hi click");
            }

    So try using the click event for the button . If you want to know more about routed events and why they will work, then please read  http://www.silverlightshow.net/items/Routed-Events-in-Silverlight.aspx

    Thursday, April 12, 2012 12:00 AM