【问题标题】:wp7 TemplateBinding Content not workingwp7 TemplateBinding 内容不起作用
【发布时间】:2012-02-16 18:41:26
【问题描述】:

我是 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”。

【问题讨论】:

    标签: c# windows-phone-7 templatebinding


    【解决方案1】:

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

    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 中的文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多