Select to view content in your preferred language

twoway databinding map.extent

1064
3
06-22-2011 08:15 PM
JoeShmoe
Deactivated User
So... I'd love to write <esri:Map Extent="{Binding myExtent, Mode=TwoWay}" />, ala MVVM, only using databinding.

I realize that Map.Extent is not a DependencyProperty, so its not dead simple.  I hacked something crazy together with Attached Properties to set Map's Extent and an EventTrigger on ExtentChanged to receive updates on Map.Extent.  I keep getting weird loops and such.  Can anybody comment on the best way to do this, using only databinding?
0 Kudos
3 Replies
JH2
by
Emerging Contributor
Hi Bill,

I created a Behavior to do this.  e.g.

<esri:Map DataContext="{Binding myData}">

    <i:Interaction.Behaviors>

        <local:EsriBindExtentBehavior Extent="{Binding myExtent}"/>

    </i:Interaction.Behaviors>


    <esri:Map.Layers>
        ...
    </esri:Map.Layers>

</esri:Map>


The behavior hooks into the Map.ExtentChanged event and listens for changes, and also writes the changes of it's Extent Dependency Property into the Map's Extent.  (Good behavior example here)  You can also then use a converter if your extent data isn't the same format as the Map's Extent property.

As you've spotted, you'll have to be careful that when you change the map's Extent property, the ExtentChanged event will fire, and so you can end up with a loop unless you code for this scenario.

I'm afraid I can't post up my whole behavior because it was developed on my client's time but the basic functionality was as described above.  Hope that helps

James
0 Kudos
dotMorten_esri
Esri Notable Contributor
The "Extent" property is not a simple property. Basically the extent you set is rarely the extent you actually get, unless the aspect ratio you set the extent to is the exact same as the aspect ratio of the map control (and in that case there can still be some minor rounding errors). This is the main reason it isn't a DependencyProperty and not two-way* bindable, because those type of properties doesn't allow this behavior.

*Note: Other controls can bind to the Map's extent (great for updating your view-model with latest known extent), but they can't set it back. Often what you would want to do is only restore the Map's extent from the ViewModel when the map loads, and then never set it on the map again through your VM (instead if you require going to a specific extent, you should use an ICommand for that).
0 Kudos
JoeShmoe
Deactivated User
The strange loop I had was indeed being caused by those Extent rounding errors.  I was attempting to zoom to a previous, known extent.  When I told the map to zoom to this previous extent, those rounding errors caused it to zoom to a *slightly* different extent, which my logic interpreted as a new zoom event.
0 Kudos