将Button的Content属性读入样式中的ControlTemplate

时间:2011-12-06 10:21:35

标签: wpf xaml wpf-controls

我有一个带有ControlTemplate按钮的样式

 <Style x:Key="ButtonStyle"
               BasedOn="{x:Null}"
               TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Rectangle x:Name="rectangle"
                               Fill="#FF04822A"
                               Stroke="{x:Null}" />
                    <TextBlock HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               FontWeight="Heavy"
                               Foreground="Black"
                               x:Name="btnText"
                               TextAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
  <Button Grid.Row="3"
                Grid.Column="5"
                Margin="4,0,4,0"
                Command="{Binding ResetCommand}"
                Content ="Reset Cells"
                Style="{StaticResource ButtonStyle}" />

我希望TextBlock每次更新时都会从按钮内容中读取。

1 个答案:

答案 0 :(得分:3)

添加模板绑定到TextBlock

Text="{TemplateBinding Content}"

您可能只想使用ContentPresenter代替(TextBlocks通常只显示文字)。