This is a migrated thread and some comments may be shown as answers.

Designtime data in MVVM user control using map

1 Answer 134 Views
Map
This is a migrated thread and some comments may be shown as answers.
David Brubacher
Top achievements
Rank 1
David Brubacher asked on 18 Dec 2014, 03:26 PM
I'm having difficulty displaying design-time data in a user control that makes heavy use of RadMap.
The method I use for design-time is similar to the Sophia Car Rental project. I set the data context in the UserControl tag as follows:

             xmlns:design="clr-namespace:XYZ.ViewModel.Design"
             d:DataContext="{d:DesignInstance d:Type=design:PlanViewModel, d:IsDesignTimeCreatable=True}"

I then create a class that inherits from my run-time view model, but is used to put some dummy data on the screen. Here I am adding an information layer and setting the background to blue so I can see it. Also note the compiler directives to keep this design-time stuff out of my release a..

#if DESIGNONLY
using System.Windows.Media;
using Telerik.Windows.Controls.Map;

namespace XYZ.ViewModel.Design
{
    public class PlanViewModel : PlanView.PlanViewModel
    {
        public override void SetControlState(bool resetToInitialState = false)
        {
            base.SetControlState(resetToInitialState);

            var layer = new InformationLayer
            {
                Background = Brushes.Blue
            };
            Layers.Add(layer);
        }
    }
}
#endif

Lastly, this happens in the SetControlState() method, which in this case is called in the base class when the map's InitializationCompleted method is called.
The Layers Collection is defined as such;

        public ObservableCollection<InformationLayer> Layers { get; private set; }

and the user control definition looks like this;

        <telerik:RadMap HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                        ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" Center="{Binding Center, Mode=TwoWay}"
                        UseDefaultLayout="False" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4"
                        x:Name="MapContainer" ItemsSource="{Binding Layers}">
            <!-- Send the mouse position to the view model so we can fake the mouse position control with our own data -->
            <telerik:EventToCommandBehavior.EventBindings>
                <telerik:EventBinding Command="{Binding MousePositionCommand}" EventName="MouseMove"
                                      PassEventArgsToCommand="True" RaiseOnHandledEvents="True"/>
            </telerik:EventToCommandBehavior.EventBindings>
            <!-- This provider gives us a blank slate that can handle UTM based data -->
            <telerik:RadMap.Providers>
                <telerik:EmptyProvider>
                    <telerik:EmptyProvider.Projection>
                        <telerik:EPSG900913Projection />
                    </telerik:EmptyProvider.Projection>
                </telerik:EmptyProvider>
            </telerik:RadMap.Providers>
        </telerik:RadMap>

If I create the InformationLayer in XAML, I see a blue map as expected. If I do it in code - nothing. This technique works perfectly with other controls, so I'm somewhat at a loss as to what is happening, especially since similar code at run time creates a map with an InformationLayer with a blue background.

Any suggestions are welcome.

1 Answer, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 23 Dec 2014, 08:45 AM
Hi David,

Can you point us to the exact demo you are looking at ? Is it an ORM / ASP Demo, are you sure it is 100% compatible with WPF ? I managed to reproduce this but my ViewModel's constructor is not even fired when I switch to design time. On a side note, please consider having visuals (InformationLayer) in ViewModel a bad practice. Usually MVVM pattern should separate Views, Data (Models) and ViewModels. When you have visuals in the ViewModels you couple the View and the ViewModel.

Regards,
Petar Mladenov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Map
Asked by
David Brubacher
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or