0

我已經在內容控件中編寫了一個帶有Int依賴屬性的內容控件。TemplateBinding + SIlverlight 4 +默認樣式

控件具有包含控件模板的默認樣式。

現在我面臨的問題是,無論是依賴屬性的值是什麼,什麼時候渲染它總是顯示我一個零

這裏的示例代碼段:


<ControlTemplate x:Key="ControlTemplate2" TargetType="My:Control"> 
<Grid x:Name="grid" Width ="128" Height="128> 
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal" Grid.Row="1"> 
    <TextBlock x:Name="tbxTileCount" DataContext="{TemplateBinding TileCount}" 
              Text="{Binding}" Margin="10,0,0,0" 
              Foreground="White" VerticalAlignment="Center" FontSize="48" FontFamily="Segoe WP"> 
<TextBlock.RenderTransform> 
<CompositeTransform/> 
</TextBlock.RenderTransform> 
</TextBlock> 
</StackPanel> 
</Grid></Grid></ControlTemplate> 

/// <summary> 
    /// Count to be displayed 
    /// </summary> 
    public int Count 
    { 
     get { return (int)GetValue(CountProperty); } 
     set { SetValue(CountProperty, value); } 
    } 

    public static readonly DependencyProperty CountProperty = 
     DependencyProperty.Register("Count", 
            typeof(int), 
            typeof(Control), 
            null); 

儘管dep endency屬性設置爲默認值,文本塊的DataContext設置爲0

我在這裏錯過了什麼?

回答

0

我可以得到這個工作。 以下是我如何使用它: 顯然對於Silverlight 4,這是將控件屬性與默認模板關聯的方式。


<ControlTemplate x:Key="ControlTemplate2" TargetType="My:Control"> 
    <Grid x:Name="grid" Width ="128" Height="128> 
     <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal" Grid.Row="1"> 
      <TextBlock x:Name="tbxTileCount" Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Count}" 
        Margin="10,0,0,0" Foreground="White" VerticalAlignment="Center" FontSize="48" FontFamily="Segoe WP"> 
       <TextBlock.RenderTransform> 
       <CompositeTransform/> 
       </TextBlock.RenderTransform> 
      </TextBlock> 
     </StackPanel> 
    </Grid> 
</ControlTemplate>