【问题标题】:How to get the button text of dynamically created button in Windows Phone 8 C#如何在 Windows Phone 8 C# 中获取动态创建的按钮的按钮文本
【发布时间】:2014-06-10 17:35:02
【问题描述】:

在我的 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 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&lt;Group&gt; groupbtn 中有按钮并将其放入groupofcontacts listbox 并且工作正常。但我想获取单击按钮的按钮文本 因为ObservableCollection&lt;Group&gt; groupbtn 有一个或多个按钮,所以我需要获取单击按钮的按钮文本。请建议我。等待回复。 谢谢。

【问题讨论】:

    标签: c# windows-phone-8


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 2013-04-11
        相关资源
        最近更新 更多