2012-02-16 64 views
1

我与WPF工作是新的,目前我试图做到以下几点:我创建了一个简单的ContenctControl(CtrpushPinContent),其中包含一个TextBlock:WP7 TemplateBinding内容不工作

<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 “ 按要求。

回答

0

在你的情况为迅速解决这个问题,你需要实现这一点:

<LJTileSources:CtrpushPinContent HorizontalAlignment="Right" Text="{TemplateBinding Content}" 
                    Margin="4" ContentTemplate="{TemplateBinding ContentTemplate}" /> 
您没有设置Text属性在你的控制

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(); 
     } 

当您在图钉模板设置文本,因为您使用名称“Text”注册了依赖项属性CaptionProperty的值。因此,当您从xaml设置文本时,您完全设置Caption属性,而不是控件的Text属性。所以你需要创建更改这个依赖属性的事件来更新textBox1中的文本。