在WPF中更改标签的样式和模板

12

我将一个标签的样式和模板更改如下:

    <Label Content="Test">
        <Label.Style>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border BorderBrush="#DDD" BorderThickness="1" CornerRadius="2" Background="#EEE" Padding="4">
                                <!--<TextBlock Text="{TemplateBinding Content}" />-->
                                <ContentPresenter Content="{TemplateBinding Content}" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Label.Style>
    </Label>

在设计时,这个代码是可以正常工作的并且设计视图中显示所有的东西(每个TextBlock或ContentPresenter);但是当我运行项目(或编译它)时,会收到以下错误信息:

无法找到类型"Control"上的静态成员"ContentProperty"。

请问有人能帮我吗?非常感谢:)

1个回答

12

TargetType="{x:Type Label}"添加到ControlTemplate中(否则它不会“知道”可用的属性)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接