Simple Style Definition in Silverlight INFO

Instead of specifying separately for each and every element how exactly it should look we can define a style and then apply it on various elements. Using styles promotes code reuse, makes our code shorter and assists with the code maintenance in the long run. Defining a style is about defining a collection of property values we can then apply to any element we select. Using styles is very similar to using CSS. In both cases we reuse a style definition.

The style is a resource. We define it as any other resource. Each style includes a collection of Setter elements .Each Setter element sets a specific property. The property must be a dependency one.

Unlike WPF we cannot apply the same style on different types of elements and we cannot use triggers in order to change the style of a given control when a specific property changes its value.

The following code sample includes the definition of a simple style that targets buttons.

<UserControl x:Class="SilverlightApplication30.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Style x:Key="CuteButton" TargetType="Button">
            <Setter Property="FontFamily" Value="Arial" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="Padding" Value="10" />
        </Style>
    </UserControl.Resources>
    <Canvas x:Name="LayoutRoot" Background="White">
        <Button Name="MyBt" Content="Click Here" Style="{StaticResource CuteButton}" />        
    </Canvas>
</UserControl>

The following video clip overviews this code sample and explains it.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences