Link to home
Start Free TrialLog in
Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to Access a Control on MainPage from a different user control named Page1

I have two pages in a Silverlight 4 application: MainPage and Page1. On MaingPage I have a RichTextBox1 within a Grid named myGrid. I would like to access  this RichTextBox1  from the ClickEvent of a Button1 on Page1 (that from a page different than the one on which RichTextBox1 lies in a Grid).
I would be grateful for advice on how to achieve this.
Avatar of saragani
saragani

Can you post your XAML of both pages?
Avatar of FaheemAhmadGul

ASKER

Thank you for your comment.
There are actually 3 pages in this simple test application. I am including the code behind files for the the three pages in the code window. I would like to be able to type something such as "Happy" programmatically (by clicking Button1 on Page1 into the RichTextBox on MainPage. However at present I am unable to access RichTextBox which lies inside a Grid on MainPage from Page1.
The XAML for all three of them is as follows:


MainPage.xaml

UserControl x:Class="QuickNavigationC.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">

    <Grid x:Name="LayoutRoot" Background="White" >

        <Border Height="312" Width="412" BorderBrush="Gray"  BorderThickness="3" >

        </Border>

        <Grid Name="ParentGrid" Height="300" Width="400" Background="#FFF2D7B7">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
               
            </Grid.RowDefinitions>
           
         
            <Grid Grid.Row="0" Margin="0,0,0,0" Height="150" Width="400" Background="#FF009600">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Grid.Row="0" Grid.Column="0" Name="cmdPage1" Click="cmdPage1_Click" Content="Go To Page 1"></Button>
            <Button Grid.Row="1" Grid.Column="0" Name="cmdPage2" Click="cmdPage2_Click" Content="Go to Page 2"></Button>
            <Button Grid.Row="2" Grid.Column="0" Name="cmdPage3" Content="Stay put"></Button>

            <RichTextBox Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="3" Name="txtNotes"></RichTextBox>
        </Grid>



            <Grid Grid.Row="1" Margin="0,0,0,0" Height="150" Width="400" Name="gridPlaceHolder">
            <Button Content="New Controls Will Load Here"></Button>
        </Grid>

        </Grid>
    </Grid>
</UserControl>



Page1.xaml

<UserControl x:Class="QuickNavigationC.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Name="Button1" Margin="0" Content="Clicking me shoud  type&quot;Happy&quot; into the Box Above" Click="Button1_Click"></Button>
    </Grid>
</UserControl>



Page2.xaml

<UserControl x:Class="QuickNavigationC.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Name="cmdHappy" Content="I am Page 2"></Button>
    </Grid>
</UserControl>

//MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QuickNavigationC
{
    public partial class MainPage : UserControl
    {

       
        public MainPage()
        {
            InitializeComponent();
        }

        private void cmdPage1_Click(object sender, RoutedEventArgs e)
        {

            txtNotes.Selection.Text = "You have clicked go to Page 1. ";
            Page1 newPage = new Page1();
            gridPlaceHolder.Children.Clear();       
            gridPlaceHolder.Children.Add(newPage);
           


        }

        private void cmdPage2_Click(object sender, RoutedEventArgs e)
        {
            txtNotes.Selection.Text = "You have clicked go to Page 2. ";
            Page2 newPage = new Page2();
            gridPlaceHolder.Children.Clear();
            gridPlaceHolder.Children.Add(newPage); 

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}



//Page1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QuickNavigationC
{
    public partial class Page1 : UserControl
    {
       
        public Page1()
        {
            InitializeComponent();
        }

       


    }
}


//Page1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QuickNavigationC
{
    public partial class Page2 : UserControl
    {
        public Page2()
        {
            InitializeComponent();
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant. This worked perfectly. I am extremely grateful. This has solved my problem. I would be very keen to understand and learn the alternative approach you have suggested also. I will post this question as a new related question and would be grateful if you could explain the alternative approach also if at all possible. Thanks.
Ok.

Glad to help  :-)