How to get the button text of dynamically created button in Windows Phone 8 C#

In my windows phone application I have created buttons dynamically like below:

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               
        }
    }
}

below is xaml code:

<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>

In the above code ObservableCollection<Group> groupbtn have buttons and put it into the groupofcontacts listbox and its working fine. but I want to get the button text of clicked button because ObservableCollection<Group> groupbtn have one or more buttons so I need to get the button text of clicked button. Kindly suggest me. waiting for reply. Thanks.

Answers


In btn_click, the sender variable should be the button object that was clicked. Cast it as a Button, then access it's Content property from there.


Need Your Help

Are there any free java/flash image editor plug-in/applets that i can include in my site?

ajax image-manipulation web-based image-editor

I am currently re-developing an e-commerce back end system and I was wondering if anyone here had come accross a free ajax script/plug-in/flash file which could allow our users to upload an image and

SQL XML - exist: is there a better way?

sql xml rdbms

We use an XML column alongside traditional columns in a couple of tables in our SQL Server 2008 R2 database with the structure as follows:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.