Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a Label's style and template in WPF

I change a Label's style and template as below:

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

in design time, this works and design view shows all thing good, (with each TextBlock or ContentPresenter); but when I run the project (or compile it), receive this error:

Cannot find the static member 'ContentProperty' on the type 'Control'.

can anyone help me please? thanks a lot ):

like image 520
amiry jd Avatar asked Jun 11 '11 17:06

amiry jd


People also ask

Where do I put Styles in WPF?

The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.

What is control template in WPF?

A template describes the overall look and visual appearance of a control. For each control, there is a default template associated with it which gives the control its appearance.

How many types of templates are there in WPF?

There are four types of templates: ControlTemplate. DataTemplate. ItemsPanelTemplate.

What is a control template?

The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.


1 Answers

Add TargetType="{x:Type Label}" to the ControlTemplate (otherwise it won't "know" what properties are available).

like image 50
H.B. Avatar answered Sep 18 '22 08:09

H.B.