Binding to InformationLayer and MVVM

3 posts, 0 answers
  1. Pete
    Pete avatar
    67 posts
    Member since:
    Sep 2012

    Posted 13 May 2011 Link to this post

    I am binding a LocationCollection to an informationlayer itemssource but it does not work. I have not been able to find an example where this has been done without using codebehind.
    I have a standard public property which I bind to:
    private LocationCollection _items;
        public LocationCollection Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
                OnPropertyChanged("Items");
            }
        }
    and I bind to this property in the xaml
    <telerik:InformationLayer
                    Name="informationLayer2"
                    Visibility="Visible"
                    ItemTemplate="{StaticResource myitemtemplate}"
                    ItemsSource="{Binding Items}">
                    <telerik:InformationLayer.DataMappings>
                        <telerik:DataMapping FieldName="Location" ValueMember="Location" />
                    </telerik:InformationLayer.DataMappings>
                </telerik:InformationLayer>

    If I add to Items in code behind (i.e.  informationLayer2.Items.Add(location1)), it works fine. If I add to the bound collection it does not work.

    I've looked at the examples, but I can't see what is wrong.

    Thanks
  2. Pete
    Pete avatar
    67 posts
    Member since:
    Sep 2012

    Posted 13 May 2011 Link to this post

    I've fixed it now thanks.
  3. DevCraft banner
  4. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 18 May 2011 Link to this post

    Hi XamlmaX,

    The problem has been fixed in the 2010 Q3. The data binding works fine with latest build of the RadMap control. You can see how it can be done here:

    http://demos.telerik.com/silverlight/#Map/DataBinding

    Unfortunately your code snippets doesn't provide enough information about how you implemented it. But I see a couple problems in your XAML:

    1. Data mapping you are using will not work at all because Location object has not property or field with name 'Location'. Actually you need not data mapping at all in your scenario.

    2. Since your data objects are Locations it is not necessary to specify binding for MapLayer.Location attachable property in the data template. But, if you are using it the telerik:MapLayer.Location="{Binding}" must be defined in the TOP level element in the data template (Grid in your case).

    Here it is code for simple implementation of the collection binding within MVVM (it adds new locations on mouse click):

    public class RadMapDataModel : INotifyPropertyChanged
    {
        private LocationCollection items;
      
        public RadMapDataModel()
        {
            this.Items = new LocationCollection();
        }
      
        public LocationCollection Items
        {
            get
            {
                return this.items;
            }
            set
            {
                this.items = value;
                OnPropertyChanged("Items");
            }
        }
      
        public event PropertyChangedEventHandler PropertyChanged;
      
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    <UserControl x:Class="ShowPinPointsCS.MainPage"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="400">
      
        <UserControl.Resources>
            <DataTemplate x:Key="Ellipse">
                <Grid telerik:MapLayer.Location="{Binding}"
                      telerik:MapLayer.HotSpot="0.5,0.5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="14" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Ellipse Width="14"
                         Height="14"
                         Fill="Red" />
                </Grid>
            </DataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <telerik:RadMap x:Name="radMap" 
                    Center="37.7,2.2"
                    ZoomLevel="8"
                    MapMouseClick="AddLocationToCollection">
                <telerik:RadMap.Provider>
                    <telerik:OpenStreetMapProvider />
                </telerik:RadMap.Provider>
                <telerik:InformationLayer x:Name="informationLayer" 
                              ItemsSource="{Binding Items}"
                              ItemTemplate="{StaticResource Ellipse}" />
            </telerik:RadMap>
        </Grid>
    </UserControl>

    private void AddLocationToCollection(object sender, MapMouseRoutedEventArgs eventArgs)
    {
        this.dataModel.Items.Add(eventArgs.Location);
    }


    Kind regards,
    Andrey Murzov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top