【问题标题】:TemplateBinding + SIlverlight 4 + Default StyleTemplateBinding + SIlverlight 4 + 默认样式
【发布时间】:2011-03-29 07:33:05
【问题描述】:

我在内容控件中编写了一个带有 Int 依赖属性的内容控件。

控件有一个默认样式,其中包含控件的模板。

现在我面临的问题是,无论依赖属性的值是什么,渲染时它总是显示为零

这里是示例代码 sn-p:


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

即使依赖属性设置为默认值,文本块的DataContext设置为0

我错过了什么?

【问题讨论】:

    标签: silverlight-4.0 contentcontrol templatebinding control-template


    【解决方案1】:

    我可以让它工作。 这是我如何让它工作的: 显然对于 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      相关资源
      最近更新 更多