1

我是使用 wpf 的新手,目前我正在尝试执行以下操作:我创建了一个包含 TextBlock 的简单 ContenctControl (CtrpushPinContent):

<ContentControl x:Class="CtrpushPinContent" ...
<Grid x:Name="LayoutRoot" Background="{x:Null}">
<Border BorderThickness="3" Name="border1" CornerRadius="15" BorderBrush="#FF070707" Margin="0,0,0,0">
                <Border BorderBrush="Silver" BorderThickness="3" Name="border2" CornerRadius="15" Background="#FF413E3E">
                    <TextBlock Name="textBlock1" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="4" Foreground="White" />
                </Border>
            </Border>
        </Grid>
</ContentControl>

cs 文件如下所示:

 public partial class CtrpushPinContent : ContentControl
    {
        public static readonly DependencyProperty CaptionProperty =
           DependencyProperty.Register("Text",
                                        typeof(string),
                                        typeof(CtrpushPinContent),
                                        new PropertyMetadata(string.Empty));

        public string Text
        {
            get { return textBlock1.Text; }
            set { textBlock1.Text = value; }
        }
        public CtrpushPinContent()
        {
            InitializeComponent();
        }
    }

在主 PhoneApplicationPage 我尝试执行以下操作:

<phone:PhoneApplicationPage.Resources>
<Style TargetType="my:Pushpin" x:Key="PushpinStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="my:Pushpin">
                        <Grid x:Name="ContentGrid">
                            <StackPanel Orientation="Vertical">
                                <Grid Background="{x:Null}" HorizontalAlignment="Right" MinHeight="31" MinWidth="29">
                                    <LJTileSources:CtrpushPinContent HorizontalAlignment="Right"  Text="{TemplateBinding Content}" Margin="4" ContentTemplate="{TemplateBinding ContentTemplate}" />
                                </Grid>
                                <Image Source="/WifiHotSpot;component/Images/blackPinNoShadow.png"  Width="54" Height="54" HorizontalAlignment="Center"></Image>
                            </StackPanel>
                        </Grid>
                        </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</phone:PhoneApplicationPage.Resources>

<Grid>
      <my:Map Margin="0,1,0,0" Name="map1" LogoVisibility="Collapsed" Height="576"  CredentialsProvider="key" ZoomLevel="2">
      <my:Pushpin Style="{StaticResource PushpinStyle}" Content="Test" Location="50.0863762,14.42814" PositionOrigin="BottomLeft"></my:Pushpin>
                    </my:Map>
</Grid> 

但是我的解决方案不起作用。我看不到任何效果

<my:Pushpin Style="{StaticResource PushpinStyle}" Content="Test" .../>

我相信问题出在样式声明中:

 <LJTileSources:CtrpushPinContent HorizontalAlignment="Right"  Text="{TemplateBinding Content}" Margin="4" ContentTemplate="{TemplateBinding ContentTemplate}" />

因为当我把它改成

<LJTileSources:CtrpushPinContent HorizontalAlignment="Right"  Text="TestText" Margin="4" ContentTemplate="{TemplateBinding ContentTemplate}" /> 

它根据需要显示“TestText”。

4

1 回答 1

0

在你的情况下,为了快速解决这个问题,你需要实现这个:

public static readonly DependencyProperty CaptionProperty =          
     DependencyProperty.Register("Text",
                                        typeof(string),
                                        typeof(CtrpushPinContent),
                                        new PropertyMetadata(string.Empty, OnTextChanged));

        public string Text
        {
            get { return textBlock1.Text; }
            set { textBlock1.Text = value; }
        }

        private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((CtrpushPinContent)sender).textBlock1.Text = (string)e.NewValue;
        }

        public CtrpushPinContent()
        {
            InitializeComponent();
        }

当您在图钉模板中设置文本时:

<LJTileSources:CtrpushPinContent HorizontalAlignment="Right"  Text="{TemplateBinding Content}" 
                                                                     Margin="4" ContentTemplate="{TemplateBinding ContentTemplate}" />

您没有在控件中设置 Text 属性,而是设置了依赖属性 CaptionProperty 的值,因为您使用名称“Text”注册了它。因此,当您从 xaml 设置 Text 时,您设置的正是 Caption 属性,而不是控件的 Text 属性。因此,您需要创建更改此依赖属性的事件以更新 textBox1 中的文本。

于 2012-11-16T17:36:06.910 回答