如何在Windows Phone 8 C中获取动态创建按钮的按钮文本#

时间:2014-06-10 08:13:14

标签: c# windows-phone-8

在我的Windows手机应用程序中,我已经动态创建了按钮,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.UserData;
using System.Windows.Media;
using Microsoft.Phone.Maps.Controls;
using System.Collections.ObjectModel;
using System.Collections;

namespace GetContacts
{
    public partial class createGroups : PhoneApplicationPage
    {
        string buttonName = "";
        public static ObservableCollection<Group> groupbtn;

        public createGroups()
        {
            InitializeComponent();
            groupbtn = new ObservableCollection<Group>();
        }


        private void btn_groupname_Click(object sender, RoutedEventArgs e)
        {
            if (tb_groupname.Text != string.Empty)
            {
                groupbtn.Add(new Group { Name = tb_groupname.Text });
                buttonName = tb_groupname.Text;
                lb_groupofcontacts.DataContext = groupbtn;
                tb_groupname.Text = string.Empty;
            }
        }
        private void btn_Click(object sender, RoutedEventArgs e) // button click Event
        {
         // some code               
        }
    }
}
下面的

是xaml代码:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="people" Style="{StaticResource PhoneTextTitle1Style}"/>
        <TextBlock Text="Groups" Margin="9,-7,0,0" Style="{StaticResource PhoneTextLargeStyle}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="24,0,0,0">
        <TextBox x:Name="tb_groupname"
             Height="90"
             Background="White" 
             Margin="0,0,125,517"

             Foreground="Blue" TextChanged="tb_groupname_TextChanged" GotFocus="tb_groupname_GotFocus" LostFocus="tb_groupname_LostFocus"/>
        <Button x:Name="btn_groupname"
                Content="Add"
                Background="AliceBlue"
                Foreground="Blue"
                FontSize="25"
                Width="120"
                Height="90"
                HorizontalAlignment="Right"
                VerticalAlignment="Top" Click="btn_groupname_Click"></Button>
        <ListBox x:Name="lb_groupofcontacts" ItemsSource="{Binding}" Margin="0,118,0,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button Name="btnGroups" Content="{Binding Name}" Width="200" Height="200" Click="btn_Click"/> // button click Event
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

在上面的代码ObservableCollection<Group> groupbtn中有按钮并将其放入groupofcontacts listbox并且工作正常。但我想获得点击按钮的按钮文字 因为ObservableCollection<Group> groupbtn有一个或多个按钮,所以我需要获取点击按钮的按钮文本。请建议我。等待回复。 感谢。

2 个答案:

答案 0 :(得分:2)

btn_click中,sender变量应该是单击的按钮对象。将其转换为Button,然后从那里访问它的Content属性。

答案 1 :(得分:1)

我想要点击事件中的按钮文本,然后我可以访问发件人对象:

Button button = sender as Button;
string buttonText = button.Text;