views:

40

answers:

1

Hi

Let's say I have a MainPage that has a button. I also have another page(Page2) that has a textbox. I woud like to add a simple text "TEXT" to the textbox in Page2 while navigating by the button from MainPage. I have a problem with getting actually opened Site. In Windows Forms applications it is solved by:

Page2 page2 = (Page2)Application.OpenedForms["Page2"]; page2.TextBox.Text = "TEXT";

How can i do it in Silverlight? HELP")

+1  A: 

I don't have a development environment handy, so my apologies for this being non-exact but this method should work for you and is the preferred way for me to do something like this.

Here is my understanding of the hierarchy using pseudo code markup:

<Page2>
    <Textbox ID="MyTextbox" />
    <Control ID="Page1">
        <Button ID="MyButton" />
    </Control>
</Page2>

With this understanding, it sounds like what you need to do is inject a handle to your TextBox into a property on your lower-level control:

// Page1 code
public TextBox TextboxToUpdateWhenYouPressTheButton { get; set; }

Click()
{
    // Perform Not Null Check and handle it somehow if it is null.

    // Set the text.
    TextboxToUpdateWhenYouPressTheButton.Text = "This is the text that goes into the textbox.";
}

Doing it this way, whenever you create your Page1, simply pass in a handle for your textbox into the new TextboxToUpdateWhenYouPressTheButton property on your inner control. Ultimately, the idea stays the same, we only access it via this property instead of directly now. This will allow your control to be used on many screens regardless of what the textbox is called. This way, for your page to "consume" the control, there is the requirement that it "registers" this textbox first.

Jaxidian
OK but at Page1 i do not see the actually opened Page2(and it is opened). The main problem is that Page2 is already Loaded and opened.
Rafal
So both Page1 and Page2 are open at the same time? I'm a bit confused here if you're clicking on a button on Page1 but Page2 is already open.
Jaxidian
The Page1 is a UserControl that has a button inside. The Page2 is a parent of Page1. However i cant call Parent property because i am using Click event witch passes: AddButtonControl_Click(object sender, RoutedEventArgs e)
Rafal
Okay, then in this case you need to do some tiny amount of pseudo dependency injection to do this in a good way. Give me a few minutes and I'll update my original answer...
Jaxidian
Okay, it's updated now. How's that?
Jaxidian
Hmm. Its not exactly what works for me...i was thinking of using VisualRoot to keep String text and binding the text to textbox. Do you know how can I call the Page that is a content of a VisualRoot?
Rafal