开发者

TemplateBinding + SIlverlight 4 + Default Style

开发者 https://www.devze.com 2023-02-20 16:15 出处:网络
I have written a content control with an Int Dependency Property in the content Control. The Control has a default style which contains the Template for the control.

I have written a content control with an Int Dependency Property in the content Control.

The Control has a default style which contains the Template for the control.

Now the problem i face is, No matter what the dependency property's value is , when rendered it always shows me a zero

Here's the sample Code snippet:


<ControlTemplate x:Key="ControlTemplate2" TargetType="My:Control">
<Grid x:Name="grid" Width ="128开发者_C百科" 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);

Eventhough the dependency property is set to default value, the DataContext of the textblock is set to 0

What have i missed here?


I could get this to work. Here's how I got it to work : Apparently For Silverlight 4, This is the way you associate control properties to the default Template.


<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消