0

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.

3 Answers3

1

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.

dowhilefor
  • 10,971
  • 3
  • 28
  • 45
  • I read about it but have no idea how to implement it..! Could you please tell me how can I do it? – user2451217 Jun 04 '13 at 11:40
  • This is no "two lines of codes and your problem is solved" answer of me. MVVM is a concept that works incredible well with WPF, WinRT, and Silverlight. But its a concept how to design and develop your application. If you want a quick and dirty solution, maybe NedStoyanovs answer goes in the right direction. – dowhilefor Jun 04 '13 at 11:49
1

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

Community
  • 1
  • 1
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
0

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.