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

Binding to information layer

2 Answers 110 Views
Map
This is a migrated thread and some comments may be shown as answers.
Henri
Top achievements
Rank 1
Henri asked on 21 Dec 2010, 02:26 PM
I have an informationlayer bound to a RadObservableCollection named Items.
At some point, i recalculate the location of each item in the radobservablecollection.

Unless i call the Reset() function (Items.Reset()) after iterating through the collection , the information layer is not updated.
So i do not add or remove items from the collection, i recalculate the location property of each item in the collection.
Is this the advised way, calling Reset()?

(the items in the radobservablecollection are plain simple objects, not framework elements)

Thanks,
Henri

2 Answers, 1 is accepted

Sort by
0
Harald Vålerhaugen
Top achievements
Rank 1
answered on 22 Dec 2010, 10:21 AM
Are you databinding to a location-property on your items in the collection? I have a collection of objects implementing INotifyPropertyChanged. When I change the location of the objects, the items will change position on the map without any extra effort. I'm not using the RadObservableCollection, but an ordinary ObservableCollection, but that should make any difference.

HaraldV
0
Henri
Top achievements
Rank 1
answered on 22 Dec 2010, 02:15 PM
Harald,
Yes I bind to a location property via DataMapping (see xaml)

The XAML:
<UserControl x:Class="SimpleMap.MainPage"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls.Map;assembly=Telerik.Windows.Controls.DataVisualization" 
    xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.DataVisualization" 
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
      
    <UserControl.Resources>
        <Style x:Key="mappinpointstyle" TargetType="telerik:MapPinPoint">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="telerik:MapPinPoint">
                        <Ellipse Fill="Orange" Stroke="Red" Width="11" Height="11"></Ellipse>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
  
        <DataTemplate x:Key="myitemtemplate">
            <telerik:MapPinPoint Style="{StaticResource mappinpointstyle}">
            </telerik:MapPinPoint>
        </DataTemplate>
      
    </UserControl.Resources>
  
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Content="Reposition Items" Click="OnClickRepositionItems"></Button>
        <Controls:RadMap Grid.Row="1" 
                        x:Name="map"
                        UseSpringAnimations="False"
                        Center="51.191863, 4.552259"
                        ZoomLevel="5">
              
            <telerik:InformationLayer 
                Name="informationLayer" 
                Visibility="Collapsed"
                ItemTemplate="{StaticResource myitemtemplate}"
                ItemsSource="{Binding Items}">
                <telerik:InformationLayer.DataMappings>
                    <telerik:DataMapping FieldName="Loc" ValueMember="Location" />
                </telerik:InformationLayer.DataMappings>
            </telerik:InformationLayer>
        </Controls:RadMap>
    </Grid>
</UserControl>


 And code behind

namespace SimpleMap
{
    public partial class MainPage : UserControl
    {
        MyViewModel _model = new MyViewModel();
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = _model;
            this.map.InitializeCompleted += (s, e) => { informationLayer.Visibility = Visibility.Visible; };
  
            MapProviderBase provider = new OpenStreetMapProvider(MapMode.Road, true);
            this.map.Provider = provider;
        }
  
        private void OnClickRepositionItems(object sender, RoutedEventArgs e)
        {
            _model.RepositionItems();
        }
    }
  
    public class Vehicle : INotifyPropertyChanged
    {
        #region Location Loc;
  
        private Location _Loc;
        private static PropertyChangedEventArgs _LocPropChangedEvent = new PropertyChangedEventArgs("Loc");
  
        public Location Loc
        {
            get { return _Loc; }
            set
            {
                if (value != _Loc)
                {
                    _Loc = value;
                    OnPropertyChanged(_LocPropChangedEvent);
                }
            }
        }
  
        #endregion
  
        private void OnPropertyChanged(PropertyChangedEventArgs evtArg)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler.Invoke(this, evtArg);
        }
        public event PropertyChangedEventHandler PropertyChanged;
  
    }
    //The view Model
    public class MyViewModel : INotifyPropertyChanged
    {
        public RadObservableCollection<Vehicle> Items { get; set; }
  
        public MyViewModel()
        {
            //Add some vehicles
            Items = new RadObservableCollection<Vehicle>();
            for (double x = -180.0; x < 180.0; x += 30.0)
            {
                for (double y = -90.0; y < 90.0; y += 30.0)
                {
                    Items.Add(new Vehicle() { Loc = new Location(y, x) });
                }
            }
        }
        private void OnPropertyChanged(PropertyChangedEventArgs evtArg)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler.Invoke(this, evtArg);
        }
        public event PropertyChangedEventHandler PropertyChanged;
  
        internal void RepositionItems()
        {
            foreach (var item in Items)
            {
                var curLoc = item.Loc;
                item.Loc = new Location(curLoc.Latitude + 1.0, curLoc.Longitude + 1.0);
            }
            Items.Reset();//WHEN I DO NOT CALL THIS, NO REPOSITION VISIBLE
        }
    }
}
Tags
Map
Asked by
Henri
Top achievements
Rank 1
Answers by
Harald Vålerhaugen
Top achievements
Rank 1
Henri
Top achievements
Rank 1
Share this question
or