user2451217
user2451217

Reputation: 1

WinRT Application

I am busy developing a WinRT Application.

I want to access the value of RichEditBox defined in page BasicPage1.xaml into the code behind the page BasicPage2.xaml i.e in BasicPage2.xaml.cs?

Is there anyway to get the value of the RichEditBox(defined in BasicPage1.xaml) in BasicPage2.xaml.cs ?

Thanks in anticipation.

Upvotes: 0

Views: 151

Answers (3)

Yannick Paalvast
Yannick Paalvast

Reputation: 1

Do you need to send it through when navigating to the other page? Then you can do it like this:

this.Frame.Navigate(typeof(BasicPage2),textbox.Text);

and at the BasicPage2.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var textbox= e.Parameter; 
    ...    
} 

But i also highly recommend using MVVM in your application. With MVVMLight you can implement this quite easy and quick.

Upvotes: 0

NeddySpaghetti
NeddySpaghetti

Reputation: 13495

A simple way to do this is to give your textbox a name in the XAML and then access that textbox via the name in the code behind.

<TextBox Name="myTextBox"/>

then in the code behind you can do this

myTextBox.Text = "blah";

A better way is to use binding so that updating the textbox automatically updates the property you are bound to. Have a look at this post textbox binding example

For a rich edit textbox you should be able to do this:

set

myTextBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, "Here is text");

get

  string value = string.Empty;
  myTextBox.Document.GetText(Windows.UI.Text.TextGetOptions.AdjustCrlf, out value);

See this post for more information

Upvotes: 1

dowhilefor
dowhilefor

Reputation: 11051

Are you familiar with MVVM? Basically the idea is to not rely to much on the control layer for business data, instead share these information on another layer, in this case the model or view model. So lets say you want to want to load a project and have a dialog with a textbox containing the path to a project, which the user can modify. So you would store the path in a model called ProjectInformation, this object you can now pass to other views (to be more precise, view models and then views) and use the data there. The important part here is lifetime, your model propably lives much longer than your view, so the data is stored and reused in the places where its necessary.

Upvotes: 1

Related Questions