简体   繁体   English

如何访问位于DataTemplate标签的文本框控件 <phone:PanoramaItem.HeaderTemplate> 从代码开始-在后面(MainPage.xaml.cs)

[英]How to access textbox control located in DataTemplate tags of <phone:PanoramaItem.HeaderTemplate> from code - behind (MainPage.xaml.cs)

i have a textbox located in MainPage.xaml : 我在MainPage.xaml中有一个文本框:

<phone:PanoramaItem.HeaderTemplate>

                <DataTemplate>                       
                    <TextBox x:Name="src_textbox" Width="400" TextChanged="src_textbox_TextChanged"/>
                </DataTemplate>
 </phone:PanoramaItem.HeaderTemplate>

now in MainPage.xaml.cs , i want to access src_textbox in the event handler src_textbox_TextChanged : 现在在MainPage.xaml.cs中,我想访问src_textbox在事件处理程序src_textbox_TextChanged

the event hanndler is as follows: 事件处理程序如下:

 void src_textbox_TextChanged(object sender, TextChangedEventArgs e)
    {

        string hello = src_textbox.Text();

    }

i get red lines under src_textbox and error says "it is out of current context" . 我在src_textbox下出现src_textbox ,错误提示“它不在当前上下文中”。

How do i access it ? 我如何使用它?

Yeah I faced the same problem. 是的,我遇到了同样的问题。 Solution: 解:

void src_textbox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox temp_textBox = sender as TextBox;

    string hello = temp_textBox.Text();

}

You don't need to add name attribute to your TextBox control in the xaml. 您无需在xaml中的TextBox控件中添加名称属性。

Happy Coding!! 快乐编码!

Try this : 尝试这个 :

private void src_textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txtpanorma = (TextBox)sender;
            Debug.WriteLine(txtpanorma.Text);
        }

TextBox.Text is a Property not a method so you cant use () for that. TextBox.Text是Property而不是method因此您不能使用()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows Phone中从MainPage.xaml.cs文件到MainPage.xaml文件获取字符串变量的值 - Get value of a string Variable from MainPage.xaml.cs file to MainPage.xaml file in Windows Phone 如何从MainPage.xaml.cs调用LoadData() - How to call LoadData() from MainPage.xaml.cs 从Windows Phone 8中的App.Xaml.cs到达MainPage.Xaml.cs单击处理程序函数 - Reaching MainPage.Xaml.cs click handler functions from App.Xaml.cs in windows phone 8 如何从App.xaml.cs调用MainPage.xaml.cs中的函数 - How to call a function that's in MainPage.xaml.cs from App.xaml.cs 在 MainPage.xaml.cs 中使用 Android 特定代码 - Use Android specific code in MainPage.xaml.cs 从另一个类ex.:MainPage.xaml.cs访问对象的最佳方法是什么? - Which is the best way to access to object from another class ex.:MainPage.xaml.cs? 从phonegap应用程序的MainPage.xaml.cs切换html页面 - Switching the html page from the MainPage.xaml.cs of a phonegap application 类在MainPage.xaml.cs上实例化2次 - Class instantiate 2 times on MainPage.xaml.cs Xamarin 从 MainPage.xaml.cs 看不到 MainPage.xaml 中的对象 - Xamarin can't see objects in MainPage.xaml from MainPage.xaml.cs 从 MainPage.xaml.cs 文件到 Xamarin 中的 MainPage.xaml 文件中获取 int 变量的值 - Get value of an int Variable from MainPage.xaml.cs file to MainPage.xaml file in Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM