user88
user88

Reputation: 1164

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.

Upvotes: 1

Views: 909

Answers (2)

user88
user88

Reputation: 1164

I want the button text from within the click event, then I can access the sender object:

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

Upvotes: 1

PulseLab
PulseLab

Reputation: 1579

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.

Upvotes: 2

Related Questions