C# 如何在WindowsPhone8C中获取动态创建的按钮的按钮文本#
在我的windows phone应用程序中,我动态创建了如下按钮:C# 如何在WindowsPhone8C中获取动态创建的按钮的按钮文本#,c#,windows-phone-8,C#,Windows Phone 8,在我的windows phone应用程序中,我动态创建了如下按钮: 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 Microso
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
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用Microsoft.Phone.UserData;
使用System.Windows.Media;
使用Microsoft.Phone.Maps.Controls;
使用System.Collections.ObjectModel;
使用系统集合;
命名空间GetContacts
{
公共分部类createGroups:PhoneApplicationPage
{
字符串buttonName=“”;
公共静态可观测集合组BTN;
公共组()
{
初始化组件();
groupbtn=新的ObservableCollection();
}
私有无效btn\u组名\u单击(对象发送者,路由目标e)
{
if(tb_groupname.Text!=string.Empty)
{
添加(新组{Name=tb_groupname.Text});
buttonName=tb_groupname.Text;
lb_groupofcontacts.DataContext=groupbtn;
tb_groupname.Text=string.Empty;
}
}
private void btn_Click(对象发送方,RoutedEventArgs e)//按钮单击事件
{
//一些代码
}
}
}
下面是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>
//按钮点击事件
在上面的代码observedcollection groupbtn
中有按钮,并将其放入groupofcontacts列表框
及其工作状态。但是我想得到点击按钮的按钮文本
因为observateCollection groupbtn
有一个或多个按钮,所以我需要获取单击按钮的按钮文本。请推荐我。等待答复。
谢谢。在
btn\u单击中,发送者
变量应该是单击的按钮对象。将其强制转换为按钮
,然后从那里访问其内容
属性。我需要单击事件中的按钮文本,然后我可以访问发件人对象:
Button button = sender as Button;
string buttonText = button.Text;