0

如何通过程序动态更改工具包图钉的内容?

我在初始化的地图上放了几个图钉,我想在用户点击图钉时更改图钉的内容。

这是我的 xaml 代码:

<toolkit:MapExtensions.Children>
    <toolkit:MapItemsControl x:Name="MapItems">
        <toolkit:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <toolkit:Pushpin GeoCoordinate="{Binding Coordinate}" Tap="PinOnTap">
                    <toolkit:Pushpin.Template>
                        <ControlTemplate TargetType="toolkit:Pushpin">
                            <StackPanel>
                                <ContentPresenter x:Name="content" HorizontalAlignment="Center" Content="{TemplateBinding Content}" />
                                <Path Data="M0,0 L0,1 L1,0" Fill="{TemplateBinding Background}" Stretch="Fill" Margin="32,0" Height="12" Width="18"
                                    Visibility="{Binding Content.Visibility, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                    HorizontalAlignment="Left" />
                                <Image Source="{Binding Icon_img}" Stretch="None" HorizontalAlignment="Left"/>
                            </StackPanel>
                        </ControlTemplate>
                    </toolkit:Pushpin.Template>
                    <Border Background="White" Visibility="Collapsed" Width="200" HorizontalAlignment="Center" >
                        <StackPanel Orientation="Horizontal" Tap="gotoProfileFromPin">
                            <Image Margin="8" Source="{Binding Image}" VerticalAlignment="Top" Width="50" Height="50" />
                            <StackPanel>
                                <TextBlock Text="{Binding Uid}" Visibility="Collapsed"/>
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </toolkit:Pushpin>
            </DataTemplate>
        </toolkit:MapItemsControl.ItemTemplate>
    </toolkit:MapItemsControl>
</toolkit:MapExtensions.Children>

还有我的program.cs:

private void PinOnTap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var popOut = ((sender as Pushpin).Content) as Border;
    popOut.Visibility = System.Windows.Visibility.Visible;

    var popOutImg = (popOutNew.Child as StackPanel).DataContext;

    var popUser = popOutImg as UserPin;

    // function to get the images by Uid

    popUser.Image = new Uri(images fetch from function, UriKind.RelativeOrAbsolute);

    popOutNew.Child.UpdateLayout();

    e.Handled = true;
}

在 PinOnTap 函数中,我将从另一个函数中获取图片并在地图上显示为 toolkit:Pushpin.Content,但是它无法工作。

这段代码:

popUser.Image = new Uri(images fetch from function, UriKind.RelativeOrAbsolute);

它只会更改图像源,但不会更新 UI。我打印它的价值并且它的来源已经改变,但我不知道为什么它没有出现在地图上(地图只显示白色背景的“边界”并且里面没有任何图像)

4

1 回答 1

0

我认为您将不得不实施 INotifyPropertyChange 因为更改未反映在 Xaml 上。

于 2013-08-16T10:34:39.523 回答