I've got a Bing Map element in UserControl1.xaml file:
<UserControl x:Class="MyMaps.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<Grid>
<m:Map CredentialsProvider="Gr8GooglyMoogly" x:Name="myMap" />
</Grid>
</UserControl>
I can access it like so from Form1, on which it sits:
this.userControl11.myMap.Mode = new RoadMode();
...but when I try to access it from another form, none of these attempts work:
userControl11.myMap.Children.Add(pin); // does not exist in the current context
Form1.userControl11.myMap.Children.Add(pin); // inaccessible due to its protection level
UserControl1.myMap.Children.Add(pin); // object reference is required for the static field, ...
How can I get a handle on the UserControl from another form?
UPDATE
Using Reza's comment to change the Modifier property of the map from Private to Public, and utilizing the method shown at the link provided, the following works:
var frmMain = new Form1();
frmMain.userControl11.myMap.Children.Add(pin);
UPDATE 2
Reza's idea worked perfectly. This is how I tested it to verify:
In "Form 2" (mdlDlgFrm_AddNewLocation):
// to access map on main form (Form1)
private Form1 frmMain;
// second constructor so as to access map on main form (to add pushpins)
public mdlDlgFrm_AddNewLocation(Form1 f1)
{
InitializeComponent();
this.frmMain = f1;
// test
AddPushpin("blaJustATest");
}
private void AddPushpin(string fullAddress)
{
Pushpin pin = new Pushpin();
// "brute-forcing" the coordinates for this test
pin.Location = new Location(37.1481402218342, -119.644248783588); // Interesting location: out in the "boondocks" between Oakhurst and Auberry
this.frmMain.userControl11.myMap.Children.Add(pin);
}
...and "Form 2" being invoked from the main form (Form 1):
private void addLocationToolStripMenuItem_Click(object sender, EventArgs e)
{
mdlDlgFrm_AddNewLocation frmAddNewLocation = new mdlDlgFrm_AddNewLocation(this);
frmAddNewLocation.ShowDialog(this);
frmAddNewLocation.Dispose()
}