How can I bind a control in MainWindow from another window in WPF?

Basically, my question appears already in the title: When I have MainWindow, as shown below:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:easycache" xmlns:map="clr-namespace:MapControl;assembly=MapControl.WPF" x:Class="easycache.MainWindow" x:Name="MainWindow1" Title="easycache" Height="600" Width="850"> <map:Map Grid.Column="2" Grid.Row="1" Name="map" IsManipulationEnabled="True" LightForeground="Black" LightBackground="White" DarkForeground="White" DarkBackground="#FF3F3F3F" Center="0.0,0.0" ZoomLevel="0.0" TileLayer="{Binding Source={StaticResource TileLayersView}, Path=CurrentItem}" /> 

In my window 2, I have:

 <map:Map Name="map" IsManipulationEnabled="False" LightForeground="Black" LightBackground="White" DarkForeground="White" DarkBackground="#FF3F3F3F" Center="0.0,0.0" ZoomLevel="{Binding ?????}"> 

And I want to bind ZoomLevel maps in Window2 to ZoomLevel maps in my MainWindow. How can I achieve this?

Regards, Ialokim

+4
source share
1 answer

In your MainWindow code, create a public property using open access for access, something like this:

 private double _mapZoom; public double MapZoom //I assume that ZoomLevel is of type double, if not use the correct type { get { return _mapZoom; } set { _mapZoom = value; OnPropertyChanged("MapZoom"); } } 

You will need to implement the INotifyPropertyChanged interface in your MainWindow (there are a million examples around the world, no need to delve into this here).

Then attach your ZoomLevel to the MapZoom property as follows:

 <map:Map Name="map" ZoomLevel="{Binding RelativeSource={RelativeSource Self}, Path=MapZoom}, Mode=TwoWay"/> 

For this to work, the DataContext of the window must be the window itself, you need one line in the Window constructor:

  public MainWindow() { InitializeComponent(); this.DataContext = this; //add this line } 

You need the second window to call the main window, so you can create a static property to return the current instance to mainwindow:

  private static MainWindow _instance; public static MainWindow Instance { get { if (_instance == null) _instance = new MainWindow(); else _instance = this; return _instance; } } //you have to make sure to create ONE instance of MainWindow before getting this property, and not to create more instances elsewhere 

Now you will have MainWindow exposing a public property that is tied to map scaling.

So, in the second window, create a property that points to this instance of the main window:

  public MainWindow Main { get { return MainWindow.Instance; } 

Finally, you can bind the scaling of the second map to the public MapZoom property, which is a member of Main:

 <map:Map Name="map2" ZoomLevel="{{Binding RelativeSource={RelativeSource Self}, Path=Main.MapZoom}, Mode=TwoWay"}"> 

In this binding, "Main" refers to an instance of MainWindow through the public property window2 (set this .DataContext = this; in window2 too) and gain access to the .MapZoom property.

By default, all objects you create in XAML are private, so you must explicitly publish them with public properties using get / set accessors to make them accessible from the outside.

Let me know if this works, I have not created a code testing application. Hello!

0
source

All Articles