【问题标题】:How to give tap (or tapped) event to dynamically created TextBlock如何为动态创建的 TextBlock 提供点击(或点击)事件
【发布时间】:2014-05-23 22:53:02
【问题描述】:
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];
        }

我想在创建文本块时动态触发事件。没有生成错误,但点击(或 UWP 的点击)事件不会触发该功能。

【问题讨论】:

    标签: c# xaml windows-phone-8 uwp


    【解决方案1】:
    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);
            }
        }
    

    此代码正在运行,已执行并检查相同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 2014-01-26
      相关资源
      最近更新 更多