WPF - TemplateBinding doesn't recognize member content

蓝咒 提交于 2019-12-07 04:00:51

问题


Alright, so I have a window with the following resources

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

I am getting an error saying that, "The member "Content" is not recognized or is not accessible." What am I doing wrong?


回答1:


You will have to define TargetType on ControlTemplate

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



回答2:


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

or

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



来源:https://stackoverflow.com/questions/19082187/wpf-templatebinding-doesnt-recognize-member-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!