FaheemAhmadGul
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.
I would be grateful for advice on how to achieve this.
Can you post your XAML of both pages?
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"></RichText Box>
</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"Happy" into the Box Above" Click="Button1_Click"></Bu tton>
</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>
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.
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"></RichText
</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.
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"Happy" into the Box Above" Click="Button1_Click"></Bu
</Grid>
</UserControl>
Page2.xaml
<UserControl x:Class="QuickNavigationC.
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();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :-)
Glad to help :-)