文本块数组在点击时获取文本

时间:2015-03-06 13:02:06

标签: c# wpf events textblock

我创建了一个Textblock数组(用于创建键盘)。当我点击一个Textblock时,我想获取Textblock文本。

keyboardButtons[i] = new TextBlock();
keyboardButtons[i].Width = 45;
keyboardButtons[i].Height = 55;
keyboardButtons[i].Text = alphabet.Substring(i, 1);
keyboardButtons[i].Foreground = new SolidColorBrush(Colors.White);
keyboardButtons[i].FontSize = 25.0;
keyboardButtons[i].TextAlignment = TextAlignment.Center;
Canvas.SetLeft(keyboardButtons[i], xPos);
Canvas.SetTop(keyboardButtons[i], yPos);
RectCanvas.Children.Add(keyboardButtons[i]);

keyboardButtons[i].Tapped += new TappedEventHandler(ButtonTap);

Tapped方法:

void ButtonTap(object sender, TappedRoutedEventArgs e)
{
}

我应该在ButtonTap方法中写什么?

2 个答案:

答案 0 :(得分:1)

尝试将发件人转换为控件并获取其Text属性

var textBlock = sender as TextBlock;
if(textBlock!=null) {
    var text = textBlock.Text;
}

答案 1 :(得分:0)

按钮控件是一个更好的选择。您可以将字母表作为Button控件的TAG属性传递。

下图显示按下按钮B(仅举例)。您可以随时添加自己的逻辑。

enter image description here

C#文件背后的代码

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    char Key = 'A';

    for (int i = 0; i < 10; i++)
    {
        Button button = new Button();
        button.Tag = Key + i;

        button.FontSize = 40;

        button.Width = 100;
        button.Height = 100;

        button.Content = (char)(Key + i);
        button.Click += button_Click;

        stackPanel1.Children.Add(button);
    }

    for (int i = 10; i < 20; i++)
    {
        Button button = new Button();
        button.Tag = Key + i;

        button.FontSize = 40;

        button.Width = 100;
        button.Height = 100;

        button.Content = (char)(Key + i);
        button.Click += button_Click;

        stackPanel2.Children.Add(button);
    }

    for (int i = 20; i < 26; i++)
    {
        Button button = new Button();
        button.Tag = Key + i;

        button.FontSize = 40;

        button.Width = 100;
        button.Height = 100;

        button.Content = (char)(Key + i);
        button.Click += button_Click;

        stackPanel3.Children.Add(button);
    }
}

//Now use a switch case to get the TAG property
private void button_Click(object sender, RoutedEventArgs e)
{
    var t = (sender as Button).Tag;

    char tag = Convert.ToChar(t);

    switch (tag)
    {
    case 'A':

        console.Text = "You typed " + tag;

        //Your code logic
        break;

    case 'B':

        console.Text = "You typed " + tag;

        //Your code logic
        break;

    case 'C':

        console.Text = "You typed " + tag;

        //Your code logic
        break;

//Rest of the alphabets
    }
}

XAML文件

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="150"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Margin="40, 200, 20, 20">
        <StackPanel x:Name="stackPanel1" Orientation="Horizontal">

        </StackPanel>
        <StackPanel x:Name="stackPanel2" Orientation="Horizontal">

        </StackPanel>
        <StackPanel x:Name="stackPanel3" Orientation="Horizontal">

        </StackPanel>
    </StackPanel>
    <TextBox x:Name="console" Height="100" Width="1000" Grid.Row="1" FontSize="50" Text="Press a Button"/>
</Grid>