Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give tap (or tapped) event to dynamically created TextBlock

public void myTextBlock1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {

        StackPanel mystack = new StackPanel() { Height = 100, Width = 200 };
        TextBlock myTextBlock1 = new TextBlock() 
            { Text = "Text Block", Width = 350, Height = 40, FontSize = 20,
              VerticalAlignment = VerticalAlignment.Center, 
              TextAlignment = TextAlignment.Center, 
              HorizontalAlignment = HorizontalAlignment.Center, };
        mystack.Children.Add(myTextBlock1);
    }


for (int r = 0; r < m; r++)
        {
            TextBlock myTextBlockr = new TextBlock() 
                { Text = "Text Block", Width = 350, Height = 40, FontSize = 20,
                  VerticalAlignment = VerticalAlignment.Center, 
                  TextAlignment = TextAlignment.Center, 
                  HorizontalAlignment = HorizontalAlignment.Center };

            if (r == 0)

            {
                myTextBlockr.Tap += new 
                   EventHandler<GestureEventArgs> (myTextBlock1_Tap);
            }
            stack1.Children.Add(myTextBlockr);
            myTextBlockr.Text = a[r];
        }

I want to trigger an event dynamically when a text block is created.There are no errors generated but the tap (or tapped for UWP) event doesn't trigger the function.

like image 591
Mihir Avatar asked Oct 01 '22 01:10

Mihir


1 Answers

public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            int m = 3;
            InitializeComponent();
            for (int r = 0; r < m; r++)
            {
                TextBlock myTextBlock = new TextBlock()
                {
                    Text = "Text Block",
                    Width = 350,
                    Height = 40,
                    FontSize = 20,
                    VerticalAlignment = VerticalAlignment.Center,
                    TextAlignment = TextAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center
                };

                //If tap event required for all text box
                myTextBlock.Tap += myTextBlock1_Tap;

                //According to your code here you have triggered tap event 
                //only for the first textblock
                if (r == 0)
                {
                    myTextBlock.Tap += new
                       EventHandler<GestureEventArgs>(myTextBlock1_Tap);
                }
                // Adding to the parent Stackpanel

                stack1.Children.Add(myTextBlock);
                myTextBlock.Text = "My textblock "+r;
            }

        }
        public void myTextBlock1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            StackPanel mystack = new StackPanel() { Height = 100, Width = 200 };
            TextBlock myTextBlock1 = new TextBlock()
            {
                Text = "Text Block",
                Width = 350,
                Height = 40,
                FontSize = 20,
                VerticalAlignment = VerticalAlignment.Center,
                TextAlignment = TextAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
            };
            mystack.Children.Add(myTextBlock1);
            // Adding to the parent Stackpanel
            stack1.Children.Add(mystack);
        }
    }

This code is working , have executed and checked the same

like image 139
user3279683 Avatar answered Oct 11 '22 12:10

user3279683