繁体   English   中英

wp7 TemplateBinding内容不起作用

[英]wp7 TemplateBinding Content not working

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

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

在您要快速解决此问题的情况下,您需要实施以下步骤:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM