Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
WPF: Control Template that is customized by properties
WhatsApp
Nipun Tomar
13y
8.7
k
0
0
25
Blog
The following example demonstrates a window that defines a custom ControlTemplate for the System.Windows.Controls.Label control. Within the ControlTemplate there is a System.Windows.Controls.Border control that uses the TemplateBinding markup extension to declare that its Background property should derive its value from the Background property of the Label element, which in this case is set to LightBlue.
For Example:
<
Window
x
:
Class
="ControlTemplate.Window1"
xmlns
=
http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns
:
x
=
http://schemas.microsoft.com/winfx/2006/xaml
Title
="ControlTemplate"
Height
="100"
Width
="180">
<
Window.Resources
>
<
ControlTemplate
x
:
Key
="labelTemplate"
TargetType
="{
x
:
Type
Label
}">
<
Border
x
:
Name
="border"
CornerRadius
="4"
BorderThickness
="3"
BorderBrush
="DarkGray"
Background
="{
TemplateBinding
Property
=Background}">
<
ContentPresenter
HorizontalAlignment
="Center"
VerticalAlignment
="Center"/>
</
Border
>
</
ControlTemplate
>
</
Window.Resources
>
<
Grid
>
<
Label
Width
="100"
Height
="24"
Margin
="4"
Content
="Custom Label"
Template
="{
StaticResource
labelTemplate
}"
Background
="LightBlue"/>
</
Grid
>
</
Window
>
Up Next
Dynamically Render And Bind One User Control In Another User Control Using MVVM Pattern In WPF
Ebook Download
View all
WPF Simplified: Build Windows Apps Using C# and XAML
Read by 1.5k people
Download Now!
Learn
View all
Membership not found