Setting ToolTips using MVVM

3 posts, 1 answers
  1. Nuno
    Nuno avatar
    11 posts
    Member since:
    Nov 2010

    Posted 02 Feb 2011 Link to this post

    Hi,

    I need to read Shapefiles manually to be able to remove some polygons. (Can't use MapShapeReader, right?)

    MainPage.xaml:
    <telerik:InformationLayer x:Name="StateLayer" ItemsSource="{Binding Source={StaticResource DataContext}, Path=States, Mode=TwoWay}">


    MapViewModel.cs:
    public List<FrameworkElement> States
    {
    get { return _states; }
    set {    
    if (this._states == value)
    return;

    _states = value;
    OnPropertyChanged(
    "States");
         }
    }
    ...

    this
    .States = _allShapesToLoad; // I'm using MVVM to bind this.States to the InformationLayer ItemsSource


    How can I set ToolTips to each polygon (State: StateName) from the ViewModel to the InformationLayer ?
    Since I can't use MapShapeReader in this scenario I can't use ToolTipFormat="STATE_NAME"

    Thank you,
    Nuno

  2. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 04 Feb 2011 Link to this post

    Hello Nuno,

    You definetely can use MapShapeReader from the view model. So you can use ToolTipFormat property. For example:

    public class MapViewModel : ViewModelBase
    {
        private MapShapeReader shapeReader = new MapShapeReader();
        private List<FrameworkElement> _states = null;
      
        public MapViewModel()
        {
            shapeReader.Source = new Uri("/Telerik.RadMap.Silverlight.Q1;component/usa_states.shp", UriKind.RelativeOrAbsolute);
            shapeReader.ToolTipFormat = "STATE_NAME";
            shapeReader.PreviewReadCompleted += new PreviewReadShapesCompletedEventHandler(this.ShapePreviewReadCompleted);
            shapeReader.Read();
        }
      
        public List<FrameworkElement> States
        {
            get { return _states; }
            set
            {
                if (this._states == value)
                    return;
      
                _states = value;
                OnPropertyChanged("States");
            }
        }
      
        private void ShapePreviewReadCompleted(object sender, PreviewReadShapesCompletedEventArgs eventArgs)
        {
            this.States = eventArgs.Items;
        }
    }

    public MainPage()
    {
        InitializeComponent();
      
        this.informationLayer.DataContext = new MapViewModel();
    }


    Kind regards,
    Andrey Murzov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
  4. Nuno
    Nuno avatar
    11 posts
    Member since:
    Nov 2010

    Posted 04 Feb 2011 Link to this post

    Hi Andrey,

    Once again, thank you very much for your wise help.

    I was using the ReadCompleted event which was forcing me to create a dummy InformationLayer on the fly so that I could have access to its items collection after ReadComplet finishes. Now I switched to the PreviewReadCompleted event since I am able to reach the items collection through eventArgs.Items and as a bonus I don't need to create the dummy Information Layer!

    To create ToolTips I was doing this way, for each State:

    ToolTip tooltip = new ToolTip();
    tooltip.Placement = System.Windows.Controls.Primitives.
    PlacementMode.Mouse; 
    tooltip.Content =
    "State: " + stateName;
    ToolTipService.SetToolTip(polygon, tooltip);


    => Now, it's just like this: shapeReader.ToolTipFormat = "STATE_NAME";

    Cheers Andrey!

    Nuno

     

     

Back to Top