This project has moved. For the latest updates, please go here.

Data binding - TargetCenter

Nov 16, 2013 at 12:37 AM
Edited Nov 16, 2013 at 12:41 AM
Hi,

I am trying to animate the map by using the TargetCenter - I want to show a list of objects one by one and I want the map to "move" smoothly from one objec to the next.

I am using WPF/MVVM (with Catel framework).

I have a ViewModel in which there is a property for the target center (e.g. VmTargetCenter) and I am animating the sequence of object via a background worker. The property implements the INotifyPropertyChanged (via the Catel infrastructure but also tried with standard implementation)

The in the XAML I have the following:
<map:Map Grid.Row="1" Name="map" Margin="2" 
                    Center="{Binding CenterMap}" 
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                    TargetCenter="{Binding VmTargetCenter}">
In my VM I am using background worker to set the VmTargetCenter property:
private void workerAnimateFriends_DoWork(object sender, DoWorkEventArgs e)
        {
            var itemsIterator = this.MapItems.GetEnumerator();           

            itemsIterator.MoveNext();

            while (itemsIterator.Current != null)
            {
                itemsIterator.Current.Visible = true;
                VmTargetCenter = itemsIterator.Current.Location;
                Thread.Sleep(1500);
                itemsIterator.Current.Visible = false;
                itemsIterator.MoveNext();
            }
        }
When I do the same with CenterMap (the property the Map.Center property is bound to) I see the map jumping to the right location. Also, if I change the Map.TargetProperty from code behind (map.TargetCenter = new Location(lat,lon);) I see the correct behavior.

Any idea why this might not work?

Thanks,

Tomer

P.S. I am using the version from Nuget - 1.8.0.0
Coordinator
Nov 16, 2013 at 9:23 AM
I have tried that and it works for me. I did however only bind the TargetCenter property, without an additional binding at the Center property, because binding Center takes precedence over TargetCenter. So you have to remove this:
    Center="{Binding CenterMap}" 
Marked as answer by tomercagan on 11/16/2013 at 1:37 AM
Nov 16, 2013 at 9:45 AM
Hi Celmens,
  1. Thanks for the prompt reply.
  2. Really awesome control (I am using it for a school assignment to present animation of Facebook posts :-)
  3. Indeed it does work after removing the Center binding.
A small point - Yesterday I did try to remove the Center property binding and started to get exceptions when the application started. I thought it has to do with the Center not bounded but after reading your answer today, and trying again, I realized that the error was due to the fact that the initial value of my VmTargetCenter was null and I guess this is not handled in the Map.TargetCenter property change code...

I'd think that this kind of scenario should be handled somehow... shouldn't be hard to fix either - should I open an issue for it?

Also, is there a way to affect the animation time?

Thanks,

Tomer
Coordinator
Nov 16, 2013 at 11:13 AM
Handling null values for the Center and TargetCenter properties would be fairly easy if it was only WPF. But the code also compiles for Silverlight and WinRT and on these platforms there are no validation callbacks on dependency properties. Anyway I'm still trying to find a good solution for this problem.

For affecting the animation time, there is a public static field Mapbase.AnimationDuration which defaults to 0.5 seconds. I guess this should be a property in the next release.
Marked as answer by tomercagan on 11/17/2013 at 10:56 AM
Coordinator
Nov 17, 2013 at 4:05 PM
Since version 1.9.0 the map control accepts null values for the Center and TargetCenter properties. These values are silently converted to new Location().
Marked as answer by tomercagan on 11/17/2013 at 10:56 AM