2010-05-10 72 views
5

我能夠使Silverlight Bing地圖接受Mousclicks並將它們轉換爲C#中的圖釘。現在,我想在PushPin旁邊顯示一個文本,作爲鼠標移過引腳時出現的描述,我不知道該怎麼做。有什麼方法可以讓我做這件事?Silverlight - 通過C#向Bing Maps中的圖釘添加文本

這是C#代碼:

public partial class MainPage : UserControl 

{ 私人MapLayer m_PushpinLayer;

public MainPage() 
{ 
    InitializeComponent(); 
    base.Loaded += OnLoaded; 
} 

private void OnLoaded(object sender, RoutedEventArgs e) 
{ 
    base.Loaded -= OnLoaded; 

m_PushpinLayer = new MapLayer(); 
x_Map.Children.Add(m_PushpinLayer); 
    x_Map.MouseClick += OnMouseClick; 
} 

private void AddPushpin(double latitude, double longitude) 
{ 
    Pushpin pushpin = new Pushpin(); 
    pushpin.MouseEnter += OnMouseEnter; 
    pushpin.MouseLeave += OnMouseLeave; 
    m_PushpinLayer.AddChild(pushpin, new Location(latitude, longitude), PositionOrigin.BottomCenter); 
} 

private void OnMouseClick(object sender, MapMouseEventArgs e) 
{ 
    Point clickLocation = e.ViewportPoint; 
    Location location = x_Map.ViewportPointToLocation(clickLocation); 
    AddPushpin(location.Latitude, location.Longitude); 
} 

private void OnMouseLeave(object sender, MouseEventArgs e) 
{ 
    Pushpin pushpin = sender as Pushpin; 

    // remove the pushpin transform when mouse leaves 
    pushpin.RenderTransform = null; 
} 

private void OnMouseEnter(object sender, MouseEventArgs e) 
{ 
    Pushpin pushpin = sender as Pushpin; 

    // scaling will shrink (less than 1) or enlarge (greater than 1) source element 
    ScaleTransform st = new ScaleTransform(); 
    st.ScaleX = 1.4; 
    st.ScaleY = 1.4; 

    // set center of scaling to center of pushpin 
    st.CenterX = (pushpin as FrameworkElement).Height/2; 
    st.CenterY = (pushpin as FrameworkElement).Height/2; 

    pushpin.RenderTransform = st; 
} 

}

回答

7

你有2種方式去:

(1)創建任何的UIElement通入PushPinLayer.AddChild。該方法的AddChild將接受和任何的UIElement,諸如包含圖像和一個TextBlock此網格:

MapLayer m_PushpinLayer = new MapLayer(); 
Your_Map.Children.Add(m_PushpinLayer); 
Image image = new Image(); 
image.Source = ResourceFile.GetBitmap("Images/Pushpin.png", From.This); 
TextBlock textBlock = new TextBlock(); 
textBlock.Text = "My Pushpin"; 
Grid grid = new Grid(); 
grid.Children.Add(image); 
grid.Children.Add(textBlock); 

m_PushpinLayer.AddChild(grid, 
    new Microsoft.Maps.MapControl.Location(42.658, -71.137), 
     PositionOrigin.Center); 

(2)創建原生對象圖釘通入PushpinLayer.AddChild,但首先設置它的模板屬性。請注意,圖釘的是ContentControls,並且具有可以在XAML中定義的資源設置模板屬性:

MapLayer m_PushpinLayer = new MapLayer(); 
Your_Map.Children.Add(m_PushpinLayer); 
Pushpin pushpin = new Pushpin(); 
pushpin.Template = Application.Current.Resources["PushPinTemplate"] 
    as (ControlTemplate); 
m_PushpinLayer.AddChild(pushpin, 
    new Microsoft.Maps.MapControl.Location(42.658, -71.137), 
     PositionOrigin.Center); 

...

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
> 
    <ControlTemplate x:Key="PushPinTemplate"> 
     <Grid> 
      <Image Source="Images/Pushpin.png" /> 
      <TextBlock Text="My Pushpin" /> 
     </Grid> 
    </ControlTemplate> 
</ResourceDictionary> 

祝你好運, 吉姆·麥柯迪

臉要面對軟件和YinYangMoney

+0

謝謝吉姆..這是非常有用的:) 它一開始沒有工作..直到我改變了網格。添加到grid.Childre n.Add ...再次感謝! – Morano88 2010-05-10 22:06:50