• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

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.
0
FaheemAhmadGul
Asked:
FaheemAhmadGul
  • 3
  • 2
1 Solution
 
saraganiCommented:
Can you post your XAML of both pages?
0
 
FaheemAhmadGulAuthor Commented:
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

0
 
saraganiCommented:
Hi, here is an example of getting to the rich text box of MainPage.

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            DependencyObject dp = this;
            while (!(dp is MainPage) && dp != null)
            {
                dp = VisualTreeHelper.GetParent(dp);
            }

            if (dp != null)
            {
                MainPage mainPage = dp as MainPage;
                mainPage.txtNotes.Selection.Text = "Helloooooooooo, la la la la";
            }
        }


This is the code of the Button1 click on Page1.


What I'm doing here is using the VisualTree to search for the parent. Since Page1 is a child of the MainPage then I can look up for the parents until I get to MainPage.



Btw, this is not the best practice.


The best practice would be using Binding and have your code written using MVVM.
(No code behind).
0
 
FaheemAhmadGulAuthor Commented:
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.
0
 
saraganiCommented:
Ok.

Glad to help  :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now