locked
Changing the contents of a text box using a call back

    Question

  • Hello people,

    I am trying to develop a windows phone 8.1 app using XAML and C#. I have  a MainPage.xaml file and its corresponding MainPage.Xaml.cs. I put up a text box whose contents I need to populate using a callback function. The Tag is like - 

    <TextBox x:Name="SampleTextBox" HorizontalAlignment="Center" Margin="28,254,128,335" TextWrapping="Wrap" VerticalAlignment="Center" Width="224" PlaceholderText="Enter your Name" Text="Enter your name" Height="41"/>

    I need to set the Text/placeholder Text via a dynamic callback function, say SetTextForTextBox() defined in the MainPage.xaml.cs

    internal string SetTextForTextBox()
    {	return "Enter your Name";
    }

    instead of hard coding it in the xaml file. Could anyone please suggest how to go about it?

    Regards,

    Sreeram

    Tuesday, May 13, 2014 12:04 PM

Answers

  • For databinding you wouldn't set the TextBox's text from the property setter. You would let the data binding system do that.

    You'd have something like:

    private string _myText;
    public string MyTextProperty
    {
       get { return _myText; }
       set { _myText = value; NotifyPropertyChanged("MyTextProperty");
    }

    Any time your code sets MyTextProperty the binding system will automatically push that to the {Binding MyTextProperty} in the Xaml.

    The error you get suggests that you used a name for your property that was already in use. Don't override the existing "Text" property but create a new name.

    --Rob

    Thursday, May 15, 2014 11:28 PM
    Owner

All replies

  • You can use

    SampleTextBox.Text = "enter your name";

    In this case, SampleTextBox in the codebehind is taken from the x:Name attribute in the XAML, so (almost) any x:Name you can use this way.


    Darin R.

    Tuesday, May 13, 2014 2:05 PM
  • Also look at data binding rather than pushing the data from the code. You can associate SampleTextBox with a property on your data object and it will automatically stay in sync with that property. See http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh758320.aspx

    <TextBox Text={Binding MyTextProperty} />

    Tuesday, May 13, 2014 2:31 PM
    Owner
  • Thanks Rob and Darin.

    I tried using the property binding and in the MainPage.xaml.cs file, I wrote the following property:

    public string MyTextProperty
    {         
        get {return SetTextForTextBox(); }         
        set {this.SampleTextBox.Text = SetTextForTextBox();}
    }

    I get an error saying that it hides an inherited member (with the same name in UserControl). Use new keyword if it was intended.

    Any ideas to go about it?

    Thanks,

    Sreeram

    Wednesday, May 14, 2014 12:34 AM
  • For databinding you wouldn't set the TextBox's text from the property setter. You would let the data binding system do that.

    You'd have something like:

    private string _myText;
    public string MyTextProperty
    {
       get { return _myText; }
       set { _myText = value; NotifyPropertyChanged("MyTextProperty");
    }

    Any time your code sets MyTextProperty the binding system will automatically push that to the {Binding MyTextProperty} in the Xaml.

    The error you get suggests that you used a name for your property that was already in use. Don't override the existing "Text" property but create a new name.

    --Rob

    Thursday, May 15, 2014 11:28 PM
    Owner