WPF - TemplateBinding无法识别成员内容

时间:2013-09-29 19:16:57

标签: wpf controltemplate

好吧,所以我有一个包含以下资源的窗口

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <TextBlock Text="{TemplateBinding Content}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

我收到一条错误消息,说“会员”内容“无法识别或无法访问。” 我做错了什么?

2 个答案:

答案 0 :(得分:11)

您必须在ControlTemplate上定义TargetType

 <ControlTemplate TargetType="Button">
     <Grid>
         <TextBlock Text="{TemplateBinding Content}"/>
     </Grid>
 </ControlTemplate>

答案 1 :(得分:2)

<Window.Resources>
<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <TextBlock Text="{TemplateBinding Button.Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Window.Resources>
<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <TextBlock Text="{TemplateBinding Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>