【问题标题】:Binding PushPin Style Text to PushPin.Text将 PushPin 样式文本绑定到 PushPin.Text
【发布时间】:2013-11-29 06:26:31
【问题描述】:

我有一个 PushPin 的样式

<Style x:Key="PushPinStyle" TargetType="Maps:Pushpin">
        <Setter Property="Width" Value="25"/>
        <Setter Property="Height" Value="39"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>                      
               <Image Source="Assets/Point.png" Stretch="Uniform" HorizontalAlignment="Left"/>
                    <TextBlock Text={Binding Text}/>
                       </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我希望 textblock 绑定到 pushpin.text

 public void AddPushpin(Location latlong, string title, string description, MapLayer layer,string pinText)
    {
        try
        {
            Pushpin p = new Pushpin()
               {
                   Tag = new Metadata()
                   {
                       Title = title,
                       Description = description
                   }
               };
       //
            p.Style =     this.Resources["PushPinStyle"] as Style;
            p.Text = pinText;                      //This should appear in pushpin on top of Image

            MapLayer.SetPosition(p, latlong);

            p.Tapped += PinTapped;

            layer.Children.Add(p);
        }
        catch (Exception ex)
        {


        }
    }

但我只能看到图像。

如何将 PushPin.text 绑定到样式的 TextBlock 并显示在样式的图像上方?

【问题讨论】:

    标签: c# windows-8 winrt-xaml bing-maps


    【解决方案1】:

    你可以使用两个中的任何一个。

    <Style x:Key="PushPinStyle" TargetType="Maps:Pushpin">
        <Setter Property="Width" Value="25"/>
        <Setter Property="Height" Value="39"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Maps:Pushpin">
                    <Grid>
                        <Image Source="Assets/Point.png" Stretch="Uniform" HorizontalAlignment="Left"/>
                        <TextBlock Text="{TemplateBinding Text}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    或

    <TextBlock Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"/>
    

    【讨论】:

    • {TemplateBinding Text} 中的文本无法识别,不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2010-11-22
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多